Grab your latest tweet with PHP without File_Get_Contents()

by Bradley on February 28, 2011

Updated Code Here!

Many thanks to Nick Heer and his code for using php to pull your most recent tweet.

Unfortunately my host does not all File_Got_Contents() so that left me with curl:

<?php
								$username = "thatbradleyguy";
								$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

								function parse_feed($feed) {
								    $stepOne = explode("<content type=\"html\">", $feed);
								    $stepTwo = explode("</content>", $stepOne[1]);
								    $tweet = $stepTwo[0];
									$tweet = htmlspecialchars_decode($tweet,ENT_QUOTES);
								    return $tweet;
								}

								//Initialize the Curl session
								$ch = curl_init();
								//Set curl to return the data instead of printing it to the browser.
								curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
								//Set the URL
								curl_setopt($ch, CURLOPT_URL, $feed);
								//Execute the fetch
								$twitterFeed = curl_exec($ch);
								//Close the connection
								curl_close($ch);

								//$twitterFeed = file_get_contents($feed);
								echo('&quot;'.parse_feed($twitterFeed).'&quot;');
								?>

Want More out of WordPress?

I can show you how to get more traffic, make WordPress do amazing things, and how to make more money with your website by converting your visitors into customers. Click the button to the right to get a free estimate.

Previous post:

Next post: