2010-10-06 24 views
0

我想在我的頁面上嵌入特定的推文,例如http://twitter.com/myusername/status/123465678我還希望能夠顯示具有特定標籤的最新推文。如何顯示特定的推文? (使用PHP/jQuery)

我試圖用Twitter API CodeIgniter Library自己做到這一點,但我需要註冊我的應用程序與Oauth。這似乎是在網頁上嵌入一條推文的過度消耗。

我寧願這樣做與PHP,但可能需要解決的jQuery版本。任何人都可以指向一個腳本,可以做到這一點?

謝謝

+0

我向oAuth註冊了,更改了變量並刷新了頁面。現在,當我查看網頁時,我將其重定向到Twitter以「允許訪問」。這太瘋狂了,當然這不是用PHP嵌入1條推文的唯一方法。 – scottgemmell 2010-10-06 14:57:57

+0

我只想在PHP頁面上顯示推文。 – scottgemmell 2010-10-06 14:58:58

回答

1
$.ajax({ 
    url: 'http://api.twitter.com/1/statuses/show/4190230693289984.json', 
    dataType: 'jsonp', 
    data: null, 
    success: function (data) { 
    // Do something with the data here. 
    // I am just logging the object to the console. 
    console.log(data); 
    } 
}); 

上面提取了單個推文的json對象(http://twitter.com/CERN/status/4190230693289984)。請注意使用'jsonp'來允許客戶端的瀏覽器訪問與您不同的域。有關更多信息,請參閱http://api.jquery.com/jQuery.ajax/

1

我認爲你可以使用公共飼料而不使用Oauth。我還沒有在一段時間與它的工作,但是這個代碼在同一時間爲我工作:

$url = "http://twitter.com/yourtwittername"; 
$h = curl_init($url); 
curl_setopt($h,CURLOPT_POST,TRUE); 
curl_setopt($h,CURLOPT_POSTFIELDS,$params); 
curl_setopt($h,CURLOPT_RETURNTRANSFER,TRUE); 
curl_setopt($h,CURLOPT_VERBOSE,1); 
curl_setopt($h,CURLOPT_HTTPHEADER,array('Expect:')); 
$response = json_decode(curl_exec($h)); 
$results = curl_getinfo($h); # to check for http response, etc. 
curl_close($h); 
// additional processing on response ... 
1

我這樣做一次使用curl,你可能需要先啓用它在你的php.ini

這裏是一個類,很可能做的工作適合你

<?php 
class TwitterGrub{ 
    private $user; 
    private $password; 

    function __construct($user, $password) { 
     $this->user = $user; 
     $this->password = $password; 
    } 

    function setUser($user) { 
     $this->user = $user; 
    } 

    // same for password 


    function twitterCapture() { 


     $ch = curl_init("https://twitter.com/statuses/user_timeline.xml"); 
     curl_setopt($ch, CURLOPT_HEADER, 1); 
     curl_setopt($ch,CURLOPT_TIMEOUT, 30); 
     curl_setopt($ch,CURLOPT_USERPWD,$this->user . ":" . $this->password); 
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
     curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     $result=curl_exec ($ch); 
     $data = strstr($result, '<?'); 

     $xml = new SimpleXMLElement($data); 

     return $xml; 
} 


function twitterDisplay($twitNum = 2){ 
    $xml = $this->twitterCapture(); 

    for($i= 0; $i<$twitNum; $i++){ 
     echo $xml->status[$i]->text;  
    } 
} 
+0

SimpleXMLElement對象([error] =>不支持基本身份驗證); – scottgemmell 2010-10-06 14:16:53

+0

「不支持基本身份驗證」這是我之前使用CodeIgniter Twitter API獲得的錯誤。我猜你現在沒有oAuth就無法做任何事情。 – scottgemmell 2010-10-06 14:18:42

+0

好像你是對的...如果你發現它,請發佈解決方案 – rabidmachine9 2010-10-07 15:35:46

1

你能不能只是解析公衆的RSS?

+0

是的,現在就做。我的問題比以前更復雜。我試圖展示一條超過20多條推文的推文,這就是爲什麼我得到錯誤,所以我開始搞亂oAuth。 – scottgemmell 2010-11-03 10:35:25

相關問題