简体   繁体   中英

How to read the JSON response by php

I got the JSON response from Facebook. How to retrieve the value of $id & $post_id ?

    $response = json_decode ( file_get_contents($graph_url) );
    $id = $response.id;
    $post_id = $response.post_id;    

        print_r( $response );   
        print_r( $id );
        print_r( $post_id );

Outputs:

stdClass Object ( [id] => 232160686920835 [post_id] => 231794566957447_232160693587501 )

No output of $id & $post_id ... ...

Your object syntax is wrong. Use:

$id = $response->id;
$post_id = $response->post_id;    

print_r( $response );   
print_r( $id );
print_r( $post_id );

Or if you prefer to work with arrays, you can put true as the second argument for json_decode:

$response = json_decode ( file_get_contents($graph_url), true );
$id = $response['id'];
$post_id = $response['post_id'];    

print_r( $response );   
print_r( $id );
print_r( $post_id );

In PHP OOP

-> is used instead of . like in Java or other languages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM