首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >wp_remote_post和cURL之间的不一致

wp_remote_post和cURL之间的不一致
EN

WordPress Development用户
提问于 2020-03-25 02:23:19
回答 1查看 399关注 0票数 0

我可以成功地使用RESTful发布cURL:

代码语言:javascript
运行
复制
$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-Key“:

代码语言:javascript
运行
复制
$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: ';
   print_r( $response );
   echo '';
   return;
}

有没有人在我的语法中发现明显的错误,或者对哪里进行故障排除有建议?我看到wp_remote_post封装了WP_Http类。

更新:

确认要发送给$headersRequests::request参数如下所示:

代码语言:javascript
运行
复制
Array
    (
        [0] => Content-Type: application/json
        [1] => Api-Key: {myapikey}
        [2] => Siteid: 99999
    )

检查Requests::request方法后,$transport (在此环境中)似乎是以下对象:

代码语言:javascript
运行
复制
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_cURLs setup_handle方法中,当设置CURLOPT_HTTPHEADER时,$headers如下所示:

代码语言:javascript
运行
复制
Array
(
    [0] => 0: Content-Type: application/json
    [1] => 1: Api-Key: a3f5be6229744000b9bc25f603e80c45
    [2] => 2: Siteid: -99
    [3] => Connection: close
)

第385行:

代码语言:javascript
运行
复制
385    curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);

因此,也许我需要发送键值对的散列,而不是json字符串的数组。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2020-03-25 05:53:14

正如上面所发现的,headers需要(现在看起来很明显)键值对,而不是json风格的键值数组:

代码语言:javascript
运行
复制
$headers = array();
$headers['Content-Type'] = 'application/json';
$headers['Api-Key'] = '{myapikey}';
$headers['Siteid'] = '99999';

body需要是json,所以要么是:

代码语言:javascript
运行
复制
'body' => "{\n    \"Username\": \"Siteowner\",\n    \"Password\": \"apitest1234\"\n}"

代码语言:javascript
运行
复制
'body' => json_encode(array( 'Username' => 'Siteowner', 'Password' => 'apitest1234' ))

好时光,好时光。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/361382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档