Wp_Remote_POST和cURL之间不一致

时间:2020-03-25 作者:MikeiLL

我可以使用cURL成功制作RESTful贴子:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, \'https://api.mindbodyonline.com/public/v6/usertoken/issue\');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\\n    \\"Username\\": \\"Siteowner\\",\\n    \\"Password\\": \\"apitest1234\\"\\n}");

$headers = array();
$headers[] = \'Content-Type: application/json\';
$headers[] = \'Api-Key: {myapikey}\';
$headers[] = \'Siteid: 999999\';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
print_r($response);
if (curl_errno($ch)) {
    echo \'Error:\' . curl_error($ch);
}
curl_close($ch);
但是,使用wp_remote_post, 未能交付;Api密钥“;在标题中:

$headers = array();
$headers[] = \'Content-Type: application/json\';
$headers[] = \'Api-Key: {myapikey}\';
$headers[] = \'Siteid: 999999\';

$response = wp_remote_post( \'https://api.mindbodyonline.com/public/v6/usertoken/issue\', array(
    \'method\' => \'POST\',
    \'timeout\' => 45,
    \'redirection\' => 5,
    \'httpversion\' => \'1.0\',
    \'blocking\' => true,
    \'headers\' => $headers,
    \'body\' => array( \'Username\' => \'Siteowner\', \'Password\' => \'apitest1234\' ),
    \'cookies\' => array()
    )
);

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   return "Something went wrong: " . $error_message;
} else {
   echo \'Response: <pre>\';
   print_r( $response );
   echo \'</pre>\';
   return;
}
有没有人发现我的语法中有明显的错误,或者对在哪里进行故障排除有什么建议?我看到了wp_remote_post 包装WP_Http

更新:

确认$headers 参数正在发送到Requests::request 如下所示:

Array
    (
        [0] => Content-Type: application/json
        [1] => Api-Key: {myapikey}
        [2] => Siteid: 99999
    )
检查到Requests::request 方法,似乎$transport (在此环境中)是以下对象:

Requests_Transport_cURL Object
(
    [headers] => 
    [response_data] => 
    [info] => 
    [version] => 468480
    [handle:protected] => Resource id #4
    [hooks:protected] => 
    [done_headers:protected] => 
    [stream_handle:protected] => 
    [response_bytes:protected] => 
    [response_byte_limit:protected] => 
)
在它里面request 方法,标头数组仍如上所述。

更新二:这很有趣。在里面Requests_Transport_cURLssetup_handle 方法,当CURLOPT_HTTPHEADER 已设置,$headers 如下所示:

Array
(
    [0] => 0: Content-Type: application/json
    [1] => 1: Api-Key: a3f5be6229744000b9bc25f603e80c45
    [2] => 2: Siteid: -99
    [3] => Connection: close
)
第385行:

385    curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
因此,我可能需要发送一个键值对哈希,而不是一个json字符串数组。

1 个回复
最合适的回答,由SO网友:MikeiLL 整理而成

如上所述,headers 需要(现在看来很明显)键值对,而不是json样式的键值数组:

$headers = array();
$headers[\'Content-Type\'] = \'application/json\';
$headers[\'Api-Key\'] = \'{myapikey}\';
$headers[\'Siteid\'] = \'99999\';
以及body 需要是json 因此,要么:

\'body\' => "{\\n    \\"Username\\": \\"Siteowner\\",\\n    \\"Password\\": \\"apitest1234\\"\\n}"

\'body\' => json_encode(array( \'Username\' => \'Siteowner\', \'Password\' => \'apitest1234\' ))
好时光,好时光。

相关推荐

对REST端点的请求在浏览器和cURL中运行良好,但在WP_REST_REQUEST中失败

我正在运行WordPress 4.9.8,带有Genesis主题(可能不相关,但…)。我希望在构建页面时(而不是通过AJAX调用调用API)将一些从REST API直接拉入的JSON写入脚本标记,以提高snappier性能。幸运的是,WP为这种情况提供了WP\\U REST\\U请求。所以我写了一些代码如下:功能。phpadd_action(\'wp_head\', \'write_json_to_script_tag\'); function write_json_to_script_tag()