【问题标题】:POST request works on cURL but not when using wp_remote_postPOST 请求在 cURL 上有效,但在使用 wp_remote_post 时无效
【发布时间】:2017-09-28 20:02:14
【问题描述】:

我正在成功执行以下请求:

curl -X POST \
  http://apitoconnect.com/v1/tracking \
  -H 'authorization: Basic dm...........PQ==' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
"CarrierName": "xxxxxx",
"OrderNumber": "111111",
"ReceiptNumber": "0000000",
"ReceiptSerie": "0",
"Code": "0000000000",
"DateToDelivery": "2017-10-03",
"CompanyDocumentNumber": "00000000",
"CustomerEmail": "xxxx@yyyy.com",
"CustomerDocumentNumber": "000000000",
"CustomerPostalCode": "0000000000",
"SendSms": "true",
"SendEmail": "true",
"CustomerPhone": "00000000000",
"CustomerName": "Bill Gates",
"SalesChannel": "00000000"
}'

效果很好!但是当我尝试使用 WordPress 进行此操作时,我总是收到错误 500。

这是我正在使用的代码:

$args = array(
    'headers' => array(
        'Authorization' => 'Basic ' . $token,
        'Content-Type'  => 'application/json'
    ),
    'body' => array(
        'OrderNumber'            => '111111',
        'ReceiptNumber'          => '0000000',
        'ReceiptSerie'           => '0',
        'Code'                   => '0000000000',
        'DateToDelivery'         => '2017-10-03',
        'CarrierName'            => 'xxxxxx',
        'CompanyDocumentNumber'  => '00000000',
        'CustomerEmail'          => 'xxxx@yyyy.com',
        'CustomerPhone'          => '00000000000',
        'CustomerName'           => 'Bill Gates',
        'CustomerDocumentNumber' => '000000000',
        'CustomerPostalCode'     => '0000000000',
        'SendSms'                => true,
        'SendEmail'              => true,
        'SalesChannel'           => '00000000',
    )
);

$response = wp_remote_post( esc_url_raw( $url ), $args );

我尝试json_encode( )参数数组,但它也不起作用......

我的代码有什么问题?

【问题讨论】:

  • 检查你的 api 一侧的 php 错误日志。它应该给你一个很好的开始提示。
  • 您是对正文还是整个 $args 进行 json_encode?似乎'body'应该只是一个JSON编码的数组,所以也许: $args["body"] = json_encode($args["body"]);
  • 我尝试的只是身体 - 'body' => json_encode( array( ... ) )。我写的方式很混乱,对不起
  • @rndus2r:API 是第三方的。我无法访问那里的任何日志...
  • 你运行的是什么版本的 WordPress?

标签: php wordpress web-services curl


【解决方案1】:

WP rest API 内外,至少在我的服务器配置上,需要手动设置用户代理。

$headers = array('Content-type' => 'application/json', 'user-agent'=>$_SERVER['HTTP_USER_AGENT']);

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2020-04-20
    • 2015-08-30
    • 2023-03-14
    • 2017-09-14
    相关资源
    最近更新 更多