json - Cannot parse results from wp_remote_get

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 =

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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

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:

  1. Make the remote GET request.
  2. Confirm that the request was successful.
  3. 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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745386615a4625467.html

相关推荐

  • json - Cannot parse results from wp_remote_get

    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 =

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信