wp_remote_post响应主体受到保护

时间:2018-07-13 06:03:17

标签: wordpress api http

我使用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);

1 个答案:

答案 0 :(得分:1)

问题

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

因为$args request 正文;因此应像这样将其传递到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()