I 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.
}
}