Show DevBest [PHP] Twitter "Latest Tweet" script.

Status
Not open for further replies.

RyanMK

Still alive
May 27, 2010
802
117
Right, this script will display your most recent tweet on twitter.
You will need to make a file called latesttweet.php and insert the code below.

PHP:
<?php
// This part pulls your twitter ID from the previous page.
$username = $twitterid;
// Prefix - some text you want displayed before your latest tweet. (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "<p>";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "<br /><a target=\"_blank\" href=\"http://twitter.com/".$twitterid."\"><em>http://twitter.com/".$twitterid."</em></a></p>";
// The Science.
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=2";
function parse_feed($feed) {
    $stepOne = explode("<content type=\"html\">", $feed);
    $stepTwo = explode("</content>", $stepOne[1]);
    $tweet = $stepTwo[0];
    $tweet = str_replace("&lt;", "<", $tweet);
    $tweet = str_replace("&gt;", ">", $tweet);
    $tweet = str_replace("&quot;", "\"", $tweet);
    $tweet = str_replace("&amp;apos;", "'", $tweet);
    return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo $prefix.parse_feed($twitterFeed).$suffix;
?>
After you've done that, put this php code on you page wherever you want it to display.

PHP:
<?php
$twitterid = "Twitter Account";
require_once('latesttweet.php');?>
Be sure to enter your twitter username in the include code above.
 

X1M!

le troller
Jan 6, 2011
179
1
Or you could just use the twitter plugin for jQuery OOOR preg_match_all and get all the tweets, ohwell, nice work.
 

RyanMK

Still alive
May 27, 2010
802
117
Or you could just use the twitter plugin for jQuery OOOR preg_match_all and get all the tweets, ohwell, nice work.
Your right you could but this was coded quickly for a mate starting a fansite and I thought what the hell and release it.
 

Bazinga

Posting Freak
Aug 3, 2010
819
54
Nice, I like it ;P Could do with some CSS though, here's my test page;
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
You could make an API out of this so that other people could use the script without having to have the code

So instead of having:
PHP:
<?php
$twitterid = "Twitter Account";
require_once('latesttweet.php');
?>

You could have:

PHP:
<?php
$twitterid = $_GET['twitterid'];
require_once( 'latesttweet.php' );
?>

Then other people could use it by having this code:
PHP:
<iframe src="http://www.site-with-twitter-api.com/get_tweet.php?twitterid=iiM4RKx"></iframe>

Just an idea, but nice code :)
 
Status
Not open for further replies.

Users who are viewing this thread

Top