Most of the bloggers(and developers), have at least once needed to
display a twitter feed on their pages. There are a variety of ways of
doing that, but today I`ll write about including a twitter feed in your
blog using PHP. The code is pretty much straight-forward. The only thing
you have to adjust is the amount of tweets you want shown and the
username, which feed you`ll be displaying.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| <?php$twitterUsername = "earthquake_jp";$amountToShow = 5;$twitterRssFeedUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$twitterUsername.'&count='.$amountToShow;$twitterPosts = false;$xml = @simplexml_load_file($twitterRssFeedUrl);if(is_object($xml)){foreach($xml->channel->item as $twit){if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){break;}$d['title'] = stripslashes(htmlentities($twit->title,ENT_QUOTES,'UTF-8'));$description = stripslashes(htmlentities($twit->description,ENT_QUOTES,'UTF-8'));if(strtolower(substr($description,0,strlen($twitterUsername))) == strtolower($twitterUsername)){$description = substr($description,strlen($twitterUsername)+1);}$d['description'] = $description;$d['pubdate'] = strtotime($twit->pubDate);$d['guid'] = stripslashes(htmlentities($twit->guid,ENT_QUOTES,'UTF-8'));$d['link'] = stripslashes(htmlentities($twit->link,ENT_QUOTES,'UTF-8'));$twitterPosts[]=$d;}}else{die('Can`t fetch the feed you requested');}?><style>a, a:link, a:visited, a:hover { color:#000;}.twitter { display:table-cell; vertical-align:middle; float:left; width:250px;}</style><!-- Insert your html here --><div class="twitter" id="jstweets"> <?php if(is_array($twitterPosts)){ echo ''; foreach($twitterPosts as $post){$data = $post['description'];echo '<a href="{$post['link']}">'.$data."<br >Updated on: ".date('l jS of F Y h:i:s A',$post['pubdate']).'</a><br ><br >'; } echo ''; }else{ echo 'No Twitter posts have been made';//Error message } ?></div> |






0 comments:
Post a Comment