2010-05-10 87 views
2

我已經將這個對象存儲在一個名爲results的變量中。例如,我將如何訪問profile_image_url。我將如何訪問此對象中的屬性? Twitter API

這裏是返回值用的print_r:

stdClass Object ( 
    [results] => Array ( 
    [0] => stdClass Object ( 
     [profile_image_url] => http://a3.twimg.com/profile_images/685278639/twitter-logo_normal.jpg 
     [created_at] => Mon, 10 May 2010 11:29:44 +0000 
     [from_user] => BarclaysWealth 
     [metadata] => stdClass Object ( 
     [result_type] => recent) 
     [to_user_id] => 
     [text] => RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7 
     [id] => 13721505359 
     [from_user_id] => 14895786 
     [geo] => 
     [iso_language_code] => en 
     [source] => <a href="http://twitter.com/">web</a> ) 
    [1] => stdClass Object ( 
     [profile_image_url] => http://a1.twimg.com/profile_images/671523082/32fb0a8_normal.jpg 
     [created_at] => Fri, 07 May 2010 21:02:10 +0000 
     [from_user] => InfoFocus 
     [metadata] => stdClass Object ( 
     [result_type] => recent) 
     [to_user_id] => 
     [text] => RT @BarclaysWealth: RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7 
     [id] => 13569384857 
     [from_user_id] => 85742792 
     [geo] => [iso_language_code] => en 
     [source] => <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> ) 
    [2] => stdClass Object ( 
     [profile_image_url] => http://a3.twimg.com/profile_images/685278639/twitter-logo_normal.jpg 
     [created_at] => Fri, 07 May 2010 20:59:20 +0000 
     [from_user] => BarclaysWealth 
     [metadata] => stdClass Object ( 
     [result_type] => recent) 
     [to_user_id] => 
     [text] => RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7 
     [id] => 13569257933 
     [from_user_id] => 14895786 
     [geo] => 
     [iso_language_code] => en 
     [source] => <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> ) 
    [3] => stdClass Object ( 
     [profile_image_url] => http://a1.twimg.com/profile_images/485934856/BD_Icon_Twitter_small_normal.gif 
     [created_at] => Fri, 07 May 2010 16:35:56 +0000 
     [from_user] => BARXdirect 
     [metadata] => stdClass Object ( 
     [result_type] => recent) 
     [to_user_id] => 
     [text] => RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7 
     [id] => 13557670463 
     [from_user_id] => 66715243 
     [geo] => 
     [iso_language_code] => en 
     [source] => <a href="http://twitter.com/">web</a> ) 
    [4] => stdClass Object ( 
     [profile_image_url] => http://a1.twimg.com/profile_images/431590520/BSL_icon_normal.gif 
     [created_at] => Fri, 07 May 2010 16:35:12 +0000 
     [from_user] => BarclaysStock 
     [metadata] => stdClass Object ( 
     [result_type] => recent) 
     [to_user_id] => 
     [text] => Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7 
     [id] => 13557633799 
     [from_user_id] => 36238006 
     [geo] => 
     [iso_language_code] => en 
     [source] => <a href="http://twitter.com/">web</a> )) 
    [max_id] => 13722716932 
    [since_id] => 0 
    [refresh_url] => ?since_id=13722716932&q=http%3A%2F%2Fbit.ly%2FOaYh7 
    [results_per_page] => 15 
    [page] => 1 
    [completed_in] => 0.026887 
    [query] => http%3A%2F%2Fbit.ly%2FOaYh7) 
+0

編輯格式化數據轉儲。 – 2010-05-10 14:25:08

回答

1

這裏有一些基本的訪問的例子:

//get a single profile url 
echo $results->results[0]->profile_image_url; 
//or 
$tweet = $results->results[0]; 
echo $tweet->profile_image_url; 

//get all the profile urls 
foreach($results->results as $tweets){ 
    echo $tweet->profile_image_url; 
} 

當然同樣的基本概念也適用於所有其它的特性,不同之處(在本具體例的數據)metadata屬性,它是一個對象。

//to get result type 
echo $results->results[0]->metadata->result_type; 
0

嘗試此:$ IMAGEURL = $ obj->結果[0] - > profile_image_url;

+0

爲什麼在這裏downvote?這是一個正確的答案。 – thetaiko 2010-05-10 16:05:41

0
$object['results'][0]->profile_image_url 

如果第一個對象是一個數組,那麼這是訪問它的方式。

+0

-1:第一個對象不是數組 – thetaiko 2010-05-10 14:13:14

0

試試這個,它會工作。

$test = $response->results[0]->id; 
print_r($test); 
+1

歡迎使用堆棧溢出。你的回答似乎沒有解決7歲的原始問題,並且已經有了一個可接受的答案。在回答新問題時,你的努力會更好。 – FluffyKitten 2017-09-05 05:26:18