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] => https://api.example.com/191/members/signup [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?