【问题标题】:Send form-data in wp_remote_post在 wp_remote_post 中发送表单数据
【发布时间】:2020-11-07 14:41:28
【问题描述】:

我有这个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)
));

有什么想法吗?

【问题讨论】:

    标签: php wordpress curl multipartform-data


    【解决方案1】:

    您正在以 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 2022-01-11
      • 2017-03-25
      • 2015-11-20
      相关资源
      最近更新 更多