2011-07-18 52 views
1

我正在使用twitter API來檢索我的所有推文。但是,我似乎無法獲得「expanded_url」和「hashtag」屬性。該特定API的文檔可在https://dev.twitter.com/docs/api/1/get/statuses/user_timeline找到。我的代碼如下:我怎樣才能通過這個數組來獲得我需要的東西?

$retweets = 'http://api.twitter.com/1/statuses/user_timeline.json? include_entities=true&include_rts=true&screen_name=callmedan'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $retweets); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$curlout = curl_exec($ch); 
curl_close($ch); 
$response = json_decode($curlout, true); 
$tweet_number = count($response); 

for($i = 0;$i < $tweet_number;$i++) 
{ 
    $url = $response['entities']['urls']; 
    $hashtag = $response['entities']['hashtags']; 
    $text = $response[$i]['text']; 

    echo "$url <br />"; 
    echo "$hashtag <br />"; 
    echo "$text <br />"; 
    echo "<br /><br />"; 

} 

我得到一個錯誤信息閱讀「公告:未定義指數:實體。」

有什麼建議嗎?

+0

'回聲的var_dump($響應);',並確保你所訪問您認爲是在那裏的數據(確保索引數組中存在)。這也是一個很好的方式來直觀地看到你想要檢索的東西。 –

回答

0

你應該做的(如果$響應是一個數組必須訪問適當的指標):

$url = $response[$i]['entities']['urls']; 
$hashtag = $response[$i]['entities']['hashtags']; 
$text = $response[$i]['text']; 

否則使用的foreach:

foreach ($response as $r){ 
    $url = $r['entities']['urls']; 
    $hashtag = $r['entities']['hashtags']; 
    $text = $r['text']; 
0

您使用的整數遞增循環,但不使用$i索引。相反,使用foreach

foreach($response as $tweet) 
{ 
    $url = $tweet['entities']['urls']; 
    $hashtag = $tweet['entities']['hashtags']; 
    $text = $tweet['text']; 

    echo "$url <br />"; 
    echo "$hashtag <br />"; 
    echo "$text <br />"; 
    echo "<br /><br />"; 

} 
相關問題