我可以使用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_cURL
ssetup_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字符串数组。