2011-05-17 32 views
-1

我正在創建一個php腳本來檢查某個人是否在Twitter上關注我,並且我正在使用REST API Method: friendships show method並使用themattHarris tmhOAuth庫進行OAuth。如何從Twitter JSON數組中提取值

我使用下面的代碼做出請求:

$code=$tmhOAuth->request('GET', $tmhOAuth->url('friendships/show'), array('target_screen_name' => 'bob')); 

if ($code==200){ $code = json_decode($tmhOAuth->response['response'], true); } 

該請求是成功的,並返回類似JSON輸出apiwiki頁所示:

{"relationship": { "source": { "id": 123, "screen_name": "bob", "following": true, "followed_by": false, "notifications_enabled": false }, "target": { "id": 456, "screen_name": "jack", "following": false, "followed_by": true, "notifications_enabled": null } } } 

我的問題

如何從返回的數組中提取以下值?

"id": 456, 
+2

你[應該在發問前搜索(http://stackoverflow.com/questions/ask-advice):http://stackoverflow.com/搜索?q =讀取+ json + php](http://stackoverflow.com/search?q=read+json+php) – Gordon 2011-05-17 21:11:11

+2

一旦您通過json_encode傳遞JSON數據,它只是一個普通的PHP數據結構。像訪問其他數組或對象一樣訪問數據。 – 2011-05-17 21:13:15

回答

0
$code['relationship']['target']['id'] 

看看例子:http://codepad.org/KMEP5wPW

+0

感謝您的快速回復。你的鍵盤示例幫助了很多。 – Andrew 2011-05-17 21:29:21

1

這只是一個PHP數組後,它已被json_decode ed,對吧?

$code->relationship->target->id 

$code['relationship']['target']['id'] 
0

您已經應用json_decode(),所以你所談論的價值應該由下面的代碼可以訪問:

$id_needed = $code['relationship']['target']['id']; 

因爲已覆蓋$code只有當$code == 200 ,您必須假設$code可能與json_decode()的結果不同。