使用PHP获取我的网站的Twitter订阅源

时间:2010-07-08 08:47:05

标签: php html twitter

我只是想知道,我会使用哪些PHP方法来获取Twitter用户的最新推文,以便在我的网站上的框中显示?

关于我如何实现这一目标的任何建议都会很棒。

欢呼声

2 个答案:

答案 0 :(得分:3)

还有一个内置的simplexml函数:

// Parse the raw xml
$xml    =    simplexml_load_file('http://twitter.com/statuses/user_timeline/{yourfeed}.rss');


// Go through each tweet
foreach ($xml->channel->item as $tweet)
{
    echo $tweet->title . '<br />';
}

答案 1 :(得分:1)

试试图书馆: http://dev.twitter.com/pages/libraries#php

或者,如果由于某些原因看起来有点过分,请通过cURL和simplexml_load_string加载Feed。根据需要进行操作。

这将显示您的上一条推文:

<?php

$ch = curl_init("http://twitter.com/statuses/user_timeline/{yourtwitterfeed}.rss");
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$data = simplexml_load_string(curl_exec($ch));
curl_close($ch);

echo $data->channel->item[0]->title;

编辑:哈。或者您可以使用simplexml_load_file(“{url goes here}”)而不是所有cURL内容。我忘了你能做到这一点。