2012-06-08 40 views
0

我正在嘗試使用某個哈希標籤(在本示例中爲#stackoverflow)獲取最近推文的文本和用戶名。返回的錯誤是Invalid argument supplied for foreach()。我認爲這是因爲$content實際上並不是一個關聯數組。使用捲曲從哈希標籤中檢索推文

function get_twitter($hashtag) { 

    $json_url = "http://search.twitter.com/search.json?q=%23" . $hashtag . "&rpp=1&result_type=recent"; 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

$returned_content = get_twitter('stackoverflow'); 
$content = json_decode($returned_content); 
foreach ($content['results'] as $tweet) { 
    echo $tweet['text']; 
    echo $tweet['from_user']; 
} 

,Twitter的返回是如下的JSON的一個例子:提前

{ 
"completed_in":0.099, 
"max_id":210895419549548544, 
"max_id_str":"210895419549548544", 
"next_page":"?page=2&max_id=210895419549548544&q=%23stackoverflow&rpp=1&result_type=recent", 
"page":1, 
"query":"%23stackoverflow", 
"refresh_url":"?since_id=210895419549548544&q=%23stackoverflow&result_type=recent", 
"results":[ 
    { 
     "created_at":"Fri, 08 Jun 2012 00:46:00 +0000", 
     "from_user":"Beseeker", 
     "from_user_id":334048944, 
     "from_user_id_str":"334048944", 
     "from_user_name":"Italo Iv\u00e1n", 
     "geo":null, 
     "id":210895419549548544, 
     "id_str":"210895419549548544", 
     "iso_language_code":"es", 
     "metadata":{"result_type":"recent"}, 
     "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1611497833\/imagen-perfil-fb_normal.jpg", 
     "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1611497833\/imagen-perfil-fb_normal.jpg", 
     "source":"<a href="http:\/\/twitter.com\/">web<\/a>", 
     "text":"podr\u00eda pasar horas y horas en #StackoverFlow", 
     "to_user":null, 
     "to_user_id":0, 
     "to_user_id_str":"0", 
     "to_user_name":null 
    } 
], 
    "results_per_page":1, 
    "since_id":0, 
    "since_id_str":"0" 
} 

感謝您的想法!

回答

2
function getTweets($hash_tag) { 

    $url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag) ; 
    echo "<p>Connecting to <strong>$url</strong> ...</p>"; 
    $ch = curl_init($url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $xml = curl_exec ($ch); 
    curl_close ($ch); 

    //If you want to see the response from Twitter, uncomment this next part out: 
    //echo "<p>Response:</p>"; 
    //echo "<pre>".htmlspecialchars($xml)."</pre>"; 

    $affected = 0; 
    $twelement = new SimpleXMLElement($xml); 
    foreach ($twelement->entry as $entry) { 
     $text = trim($entry->title); 
     $author = trim($entry->author->name); 
     $time = strtotime($entry->published); 
     $id = $entry->id; 
     echo "<p>Tweet from ".$author.": <strong>".$text."</strong> <em>Posted ".date('n/j/y g:i a',$time)."</em></p>"; 
    } 

    return true ; 
} 

使用這樣的:

getTweets('#yankees'); 

Documentation

+0

哇。這真是太棒了。謝謝你,先生! – JamieHoward

+0

不工作了。 – GusDeCooL

0

的錯誤是在你的 「foreach」 循環。它應該是:

foreach($status->results as $tweet) { 
    echo $tweet->text; 
    echo $tweet->from_user; 
} 
3

此函數不'用戶oAuth並可能不再工作。