所以我的问题是直到现在我在我的一个 wordpress 插件中使用 cURL
对于 POST
请求,但现在我需要使用 wp_remote_post()
.wp_remote_post
看起来很简单,但我无法让它工作。所以我的问题是:有人可以告诉我以下 cURL
的方式吗?可转至wp_remote_post
?
curl :
$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);
我的 wp_remote_post 版本
$result = wp_remote_post($url, array(
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($fields) )
);
我收到
wp_remote_post
的 401 错误因为授权无效。
最佳答案
我解决了。出于某种原因,在添加了 httpversion 和 sslverify 之后,它现在可以工作了。希望这有助于某人:
$result = wp_remote_post($url, array(
'method' => 'POST',
'headers' => $headers,
'httpversion' => '1.0',
'sslverify' => false,
'body' => json_encode($fields))
);
关于php - WordPress cURL 和 wp_remote_post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27919014/