admin管理员组文章数量:1318976
I am using wp_remote_post
to send a request to a 3rd parti API. The response from the API looks something like:
{
status: 'a',
member_id: 12345678
}
I understand that this is returned in the body
of the array returned by wp_remote_post
, but I'm having problems checking the value of status
. here's what I've got:
$response = wp_remote_post($url, $myArr);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Status: ' . json_decode($response['body'])['status'];
}
This results in NOTHING being echo
ed. I know the call was successful because I receive the email notification from the 3rd party saying a member was just updated (and I can see the updates in their app). If I change echo 'Status: ' . json_decode($response['body'])['status'];
to echo '<pre>' . print_r($response['body']) . '</pre>';
, the object above is printed to screen, so I'm guessing I'm doing something wrong with json_decode
.
here's the full response, in case it helps:
Array
(
[headers] => Requests_Utility_CaseInsensitiveDictionary Object
(
[data:protected] => Array
(
[accept-ranges] => bytes
[age] => 0
[content-type] => application/json; charset=utf8
[date] => Fri, 16 Oct 2020 16:43:59 GMT
[server] => Apache
[via] => 1.1 varnish (Varnish/5.2)
[x-varnish] => 1016917066
[content-length] => 48
)
)
[body] => {
"status": "a",
"member_id": 14958539705
}
[response] => Array
(
[code] => 200
[message] => OK
)
[cookies] => Array
(
)
[filename] =>
[http_response] => WP_HTTP_Requests_Response Object
(
[response:protected] => Requests_Response Object
(
[body] => {
"status": "a",
"member_id": 14958539705
}
[raw] => HTTP/1.1 200 OK
accept-ranges: bytes
age: 0
Content-Type: application/json; charset=utf8
Date: Fri, 16 Oct 2020 16:43:59 GMT
Server: Apache
via: 1.1 varnish (Varnish/5.2)
x-varnish: 1016917066
Content-Length: 48
Connection: Close
{
"status": "a",
"member_id": 14958539705
}
[headers] => Requests_Response_Headers Object
(
[data:protected] => Array
(
[accept-ranges] => Array
(
[0] => bytes
)
[age] => Array
(
[0] => 0
)
[content-type] => Array
(
[0] => application/json; charset=utf8
)
[date] => Array
(
[0] => Fri, 16 Oct 2020 16:43:59 GMT
)
[server] => Array
(
[0] => Apache
)
[via] => Array
(
[0] => 1.1 varnish (Varnish/5.2)
)
[x-varnish] => Array
(
[0] => 1016917066
)
[content-length] => Array
(
[0] => 48
)
)
)
[status_code] => 200
[protocol_version] => 1.1
[success] => 1
[redirects] => 0
[url] =>
[history] => Array
(
)
[cookies] => Requests_Cookie_Jar Object
(
[cookies:protected] => Array
(
)
)
)
[filename:protected] =>
[data] =>
[headers] =>
[status] =>
)
)
Can anyone tell me the correct way to get the value of the status
property returned in the body
object of the response?
本文标签: jsonProblems getting values from response from wpremotepost
版权声明:本文标题:json - Problems getting values from response from wp_remote_post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742056368a2418319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论