【问题标题】:wp_remote_post response body is protectedwp_remote_post 响应正文受保护
【发布时间】:2018-07-15 20:21:18
【问题描述】:

我使用wp_remote_post 进行了API 调用,并尝试使用wp_remote_retrieve_body 检索正文。但是响应只显示标题和正文受到保护。

下面的代码,我曾经做过一个API调用。

$args = array(
                'LocationId' => $loc_id,
                'AppId' => $app_id
               );
    $response = wp_remote_post( 'https://www.apiurl.com?APIKEY='."$api_key".'', $args );

$responceData = json_decode(wp_remote_retrieve_body( $response ), TRUE );
print_r($response);

打印的响应(刚刚开始的数据)是:

Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary 对象 ( [data:protected]

我正在 localhost 中开发 wordpress 插件。如何解决这个错误。

完整回复:

Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary Object ( [data:protected] => Array ( [cache-control] => no-cache, no-store [pragma] => no-cache [content-type] => application/json; charset=utf-8 [expires] => -1 [server] => Microsoft-IIS/10.0 [access-control-allow-origin] => * [x-aspnet-version] => 4.0.30319 [x-powered-by] => ASP.NET [date] => Fri, 13 Jul 2018 06:24:16 GMT [content-length] => 1858 ) ) [body] => {"Already10Questions":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"AlreadyEmailErrorMessages":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"AuthorizationError":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"ErrorMessages":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"ExistPhoneNumber":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"SuccessMsg":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"UserRegisterSuccessmessages":{"Count":1,"ResponseErrors":[{"Already10Questions":null,"AlreadyEmailErrorMessage":null,"AuthorizationError":null,"CaseNo":"[180712-23:24:16]_3D94D663C0DB","ErrorMessage":null,"SuccessMsg":null,"UserRegisterSuccessMsg":null}]},"CheckUserLocationAccessByLocationIdList":null,"ItemsCount":0,"RestaurantTotalCountByFilterList":null,"appDetails":null} 

更新:问题已通过以下代码解决。据我了解,问题在于以错误的方式向 API 发送参数。 感谢Sally Cj的帮助

$postData = array(
    'LocationId' => $loc_id,
    'AppId' => $app_id,
);
$context = array(
        'method' => 'POST',
        'headers' => "Authorization:\r\n".
            "Content-Type: application/json\r\n",
        'httpversion' => '1.0',
        'redirection' => 5,
        'timeout' => 60,
        'blocking' => true,
        'body' => json_encode($postData)
    );
            $response = wp_remote_post( 'apiurl.com?APIKEY='."$api_key".'', $context);
            $currency = wp_remote_post( 'apiurl.com?APIKEY='."$api_key".'', $context);

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
    $responceData = json_decode(wp_remote_retrieve_body($response), true);
    $currencyData = json_decode(wp_remote_retrieve_body($currency), true);

【问题讨论】:

  • 你能打印$response吗?
  • Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary Object ( [data:protected] print_r ($response);打印的回复
  • @SudharshanNair 你要求完整回复吗???
  • 您能否也发布有关问题的完整回复?
  • @SudharshanNair 我已更新完整回复。

标签: wordpress api http


【解决方案1】:

问题

不是关于响应主体被“保护”的。但是您没有得到预期的响应 data 因为 request 正文是 incomplete - 即它确实 not 包含$args 中的数据(例如AppId)。

因为$argsrequest 主体;所以它应该像这样传递给wp_remote_post()

$response = wp_remote_post( 'https://www.apiurl.com?APIKEY='."$api_key".'', array(
    'body' => $args
) );

https://codex.wordpress.org/Function_Reference/wp_remote_post#Parameters

固定密码

(为清楚起见重新缩进;还有其他更改)

// Request body.
$body = array(
    'LocationId' => $loc_id,
    'AppId'      => $app_id
);
$response = wp_remote_post( 'https://www.apiurl.com?APIKEY='."$api_key".'', array(
    'body' => $body
) );

// Response body.
$body = wp_remote_retrieve_body( $response );

// On success (i.e. `$body` is a valid JSON string), `$responceData` would be an
// `ARRAY`.
$responceData = ( ! is_wp_error( $response ) ) ? json_decode( $body, true ) : null;
var_dump( $responceData );

补充说明

这里,$responceData 将是 array,因为您将 second 参数设置为 true

$responceData = json_decode(wp_remote_retrieve_body( $response ), TRUE );

因此,如果您希望 $responceData 成为 object,则只需省略第二个参数:

$responceData = json_decode(wp_remote_retrieve_body( $response ) );
//$responceData = json_decode(wp_remote_retrieve_body( $response ), false ); // equivalent to above

有关详细信息,请参阅PHP manual for json_decode()

【讨论】:

  • 用这个 [code] (pastebin.com/AT3XCziN) 解决了这个问题。非常感谢您的回复。
  • 我想昨天我错过了Content-Type: application/json 标题.. 但无论如何,也感谢您接受我的回答。 ?
猜你喜欢
  • 2014-08-07
  • 2020-02-25
  • 2015-11-19
  • 2021-07-19
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多