gpt4 book ai didi

wordpress - wp_remote_post 响应体被保护

转载 作者:可可西里 更新时间:2023-11-01 17:22:48 27 4
gpt4 key购买 nike

我已经使用 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 Object ( [data:protected]

我正在本地主机上开发 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);

最佳答案

问题

不是响应主体被“保护”。但是您没有得到预期的响应数据,因为请求正文不完整 — 即它包含$args 中的数据(例如 AppId)。

因为 $args请求主体;所以它应该像这样传递给 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 将是一个数组,因为您将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()了解更多详情。

关于wordpress - wp_remote_post 响应体被保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51318532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com