【发布时间】: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 我已更新完整回复。