简体   繁体   English

POST 请求在 cURL 上有效,但在使用 wp_remote_post 时无效

[英]POST request works on cURL but not when using wp_remote_post

I'm executing successfully the follow request:我正在成功执行以下请求:

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"
}'

It works just fine!它工作得很好! But when I try to make this using WordPress, I always get error 500.但是当我尝试使用 WordPress 进行此操作时,我总是收到错误 500。

This is the code I'm using:这是我正在使用的代码:

$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 );

I tried to json_encode( ) the array of parameters, but it didn't work too...我试图json_encode( )参数数组,但它也不起作用......

What is wrong with my code?我的代码有什么问题?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM