我使用 PHP 来获取用户的推文。我假设它也可以用 JS 完成。
这是我使用的:
function getTweets($userid,$x){
$url = "http://api.twitter.com/1/statuses/user_timeline/$userid.xml?count=$x";
$xml = simplexml_load_file($url) or die('Could not get tweets');
echo '<ul>';
foreach($xml->status as $status){
echo grabTweetData($status);
}
echo '</ul>';
}
function grabTweetData($status) {
$id = $status->id;
$user = $status->screen_name;
$text = twitterify( $status->text );
$timestamp = $status->created_at;
$date = substr($timestamp, 0, 10).', '.substr($timestamp, -4).' at '.substr($timestamp, 11, 5);
$tweet = '<a href="https://twitter.com/'.$user.'/status/'.$id.'"><li>';
$tweet .= '<div class="ttext">'.utf8_decode($text).'</div>';
$tweet .= '<div class="tdate">Posted on: '.$date.'</div>';
$tweet .= '</li></a>';
return $tweet;
}
function twitterify($ret) {
//links #tags and @users
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
$ret = preg_replace("/#(\w+)/", "<a href=\"http://twitter.com/search?q=\\1&src=hash\" >#\\1</a>", $ret);
return $ret;
}