在 wp_remote_post 中发送表单数据

Send form-data in wp_remote_post

我有这个 curl 请求,有效:

curl -X POST "https://peyk.uk/api/v1/get-auth-token"
     -F "client_id=xxxxx"
     -F "client_secret=xxxx"

但是当尝试使用 wp_remote_post 重现时,我一直收到代码 0 响应,这根本没有帮助。

我尝试了很多不同的组合,但目前我的请求如下所示:

$body = json_encode(array(
  'client_id' => $this->settings['client_id'],
  'client_secret' => $this->settings['client_secret']
));

$result = wp_remote_post('https://peyk.uk/api/v1/get-auth-token', array(
  'method' => 'POST',
  'headers' => array('Content-Type' => 'multipart/form-data'),
  'body'=> array('formdata' => $body)
));

有什么想法吗?

您正在以 JSON 格式发送正文,而您的服务器希望它像表单字段一样编码。输入:

$result = wp_remote_post('https://peyk.uk/api/v1/get-auth-token', array(
  'method' => 'POST',
  'headers' => array('Content-Type' => 'multipart/form-data'),
  'body' => array(
    'client_id' => $this->settings['client_id'],
    'client_secret' => $this->settings['client_secret']
  )
));

详情见documentation