Skip to content

Instantly share code, notes, and snippets.

@marchawkins
Created February 22, 2013 15:48
Show Gist options
  • Save marchawkins/5014346 to your computer and use it in GitHub Desktop.
Save marchawkins/5014346 to your computer and use it in GitHub Desktop.
How-to display your latest twitter tweets on your website with php. Code tutorial here: http://www.marchawkins.com/notes/twitter-on-your-website
<?php
/* if you're having trouble, uncomment this section to show php errors/warnings
error_reporting(E_ALL);
ini_set('display_errors', '1');
*/
// create an array to hold the tweets
$tweets = array();
// enter your twitter username in the screen_name parameter below
// optionally, add exclude_replies=true to remove any of your twitter replies from your feed
$tweetFeed = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=YOURTWITTERUSERNAME&exclude_replies=true';
$xml = simplexml_load_file($tweetFeed);
$tweetXML = $xml->status;
for($i=0; $i<count($tweetXML); $i++) {
$myTweet = mb_convert_encoding($tweetXML[$i]->text[0], "HTML-ENTITIES", "UTF-8");
array_push($tweets, $myTweet);
}
// set the maximum number of tweets to display on the page
$maxTweets = 5;
?>
<html>
<head>
<title>latest tweets from twitter</title>
<style>
body {font-family: verdana,helvetica,sans-serif; font-size: 9pt;}
ul {list-style: none;}
li {display: block; margin: 1%;}
</style>
</head>
<body>
<?php if(count($tweets)>0) { ?>
<ul>
<?php for($i=0; $i<$maxTweets; $i++) { ?>
<li><?= preg_replace('/((www|http:\/\/)[^ ]+)/', '<a href="\1">\1</a>', $tweets[$i]) ?></li>
<?php } ?>
</ul>
<?php } ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment