开发者

Which is most prefered and lightweight way to show your Last Tweet using php?

开发者 https://www.devze.com 2023-02-12 17:20 出处:网络
I need to Display Last Tweet on a web-page and want to control the styling of tweet from CSS. What is the good way to do this and Tweet should also be seen even if Javascript is disabled.

I need to Display Last Tweet on a web-page and want to control the styling of tweet from CSS. What is the good way to do this and Tweet should also be seen even if Javascript is disabled.

Page is on PHP server.

Edit

I just want to show last tweet on a page like th开发者_C百科is.

<div id="last-tweet">
<p>Content of tweet here</p>
</div>


With the Twitter REST API.

$api_url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=USERNAME';
$twitter_data = file_get_contents($api_url);
$twitter_data = json_decode($twitter_data);
echo '<div id="last-tweet"><p>' . $twitter_data[0]->text . '</p></div>';

Replace USERNAME with your username.

http://dev.twitter.com/doc#rest-api

http://php.net/file-get-contents

http://php.net/json-decode


Building on Jonah's answer, here's how to do the exact same but automatically link to other mentioned users and links:

$api_url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=YOURUSERNAME';
$twitter_data = file_get_contents($api_url);
$twitter_data = json_decode($twitter_data);
$tweet = '<div id="last-tweet"><p>' . $twitter_data[0]->text . '</p></div>';

// Turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $tweet);

// Turn @replies into links
$tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);

echo $tweet;
0

精彩评论

暂无评论...
验证码 换一张
取 消