I'm calling an api with wp_remote_get
and I can't seem to parse the results.
Here is how I call the api:
$url = 'https://blah/api/data/';
$args = array(
'headers' => array(
'Authorization' => 'Token ' . $my_token,
),
);
$data = wp_remote_get($url, $args);
$data = json_encode($data);
The API response looks something like this:
`{
"headers": {},
"body": "[{\"name\":\"bob\",\"email\":\"[email protected]\",\"first_name\":\"Bob\",\"last_name\":\"Bob\"}]"
"response": {
"code": 200,
"message": "OK"
},
"cookies": [],
"filename": null,
"http_response": {
"data": null,
"headers": null,
"status": null
}
}`
Now, I want loop through the body, which has data I would like to store. When I try this:
foreach($data["body"] as $datapoint){
//blah
}
I get the following error: PHP Warning: Illegal string offset 'body'
and I am unable to loop through the data. I can't' seem to figure it out, shouldn't using json_encode allow me to treat the response as a json object?
Any help is appreciated! Thank you
I'm calling an api with wp_remote_get
and I can't seem to parse the results.
Here is how I call the api:
$url = 'https://blah/api/data/';
$args = array(
'headers' => array(
'Authorization' => 'Token ' . $my_token,
),
);
$data = wp_remote_get($url, $args);
$data = json_encode($data);
The API response looks something like this:
`{
"headers": {},
"body": "[{\"name\":\"bob\",\"email\":\"[email protected]\",\"first_name\":\"Bob\",\"last_name\":\"Bob\"}]"
"response": {
"code": 200,
"message": "OK"
},
"cookies": [],
"filename": null,
"http_response": {
"data": null,
"headers": null,
"status": null
}
}`
Now, I want loop through the body, which has data I would like to store. When I try this:
foreach($data["body"] as $datapoint){
//blah
}
I get the following error: PHP Warning: Illegal string offset 'body'
and I am unable to loop through the data. I can't' seem to figure it out, shouldn't using json_encode allow me to treat the response as a json object?
Any help is appreciated! Thank you
Share Improve this question asked Jun 20, 2019 at 0:33 ellenellen 3451 gold badge5 silver badges16 bronze badges2 Answers
Reset to default 0I can't' seem to figure it out, shouldn't using json_encode allow me to treat the response as a json object?
It does allow you to treat it as a JSON object, but that's useless in PHP. You want the result to be turned from JSON into a PHP array.
The required steps are:
- Make the remote GET request.
- Confirm that the request was successful.
- Get the body of the response and decode the JSON into a PHP array.
That would look like this:
$request = wp_remote_get(
'https://blah/api/data/',
[
'headers' => [
'Authorization' => 'Token ' . $my_token,
],
]
);
if ( ! is_wp_error( $request ) ) {
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
foreach ( $data as $datapoint ) {
echo $datapoint->name;
echo $datapoint->email;
// etc.
}
}
It should be json_decode
. You can also try the following function from https://codex.wordpress/Function_Reference/wp_remote_retrieve_body:
$url = 'https://blah/api/data/';
$args = array(
'headers' => array(
'Authorization' => 'Token ' . $my_token,
),
);
$data = wp_remote_get($url, $args);
/* Will result in $api_response being an array of data,parsed from the JSON response of the API listed above */
$api_response = json_decode( wp_remote_retrieve_body( $data ), true );