2013-02-17 130 views
1

我得到了來自Facebook的JSON響應。如何檢索$id & $post_id的值?如何閱讀php的JSON響應

$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); 

輸出:

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

$id & $post_id ... 無輸出...

+0

你錯誤PHP的JavaScript;)。只需使用'$ id = $ response-> id' – Broncha 2013-02-17 17:53:22

回答

4

你的對象語法是錯誤的。用途:

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

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

或者,如果你喜歡用數組的工作,你可以把true作爲第二個參數爲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); 
+0

謝謝!我得到了正確的ID。 – 2013-02-18 02:45:57

0

在PHP OOP

->用來代替.像用Java或其他語言編寫。