WordPress cURL 和 wp_remote_post

Den*_*ebi 3 php wordpress post curl

所以我的问题是,直到现在我在我的一个 wordpress 插件中使用cURL了一个POST请求,但现在我需要使用wp_remote_post().

wp_remote_post看起来很简单,但我无法让它工作。所以我的问题是:有人可以告诉我如何将以下内容cURL转移到wp_remote_post

卷曲:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

我的 wp_remote_post 版本

$result = wp_remote_post($url, array(
    'method' => 'POST',
    'headers' => $headers,
    'body' => json_encode($fields) )
);
Run Code Online (Sandbox Code Playgroud)

我收到 401 错误,wp_remote_post因为授权无效。

Den*_*ebi 7

我解决了。出于某种原因,在添加了 httpversion 和 sslverify 之后,它现在可以工作了。希望这有助于某人:

$result = wp_remote_post($url, array(
        'method' => 'POST',
        'headers' => $headers,
        'httpversion' => '1.0',
        'sslverify' => false,
        'body' => json_encode($fields))
    );
Run Code Online (Sandbox Code Playgroud)