skip navigation

PHP: Displaying Twitter Search Results in HTML

Here's a new use for our RSS Feed Reader - displaying content returned by the Twitter API. In this article we are simply fetching and presenting the most recent 'tweets' containing a particular string, but there's no reason why the same code can't be used for other RSS content returned by Twitter.

Fetching the feed contents from Twitter

The first step is to define our parameters:

<?PHP if(!isset($_GET['q']) || !$q = $_GET['q']) $q = 'twitter'; $NUMITEMS = 10; $BLOGURL = "http://search.twitter.com/search.rss?q=" . urlencode($q) , "&rpp=$NUMITEMS"; $TIMEFORMAT = "j F Y, g:ia"; $CACHEFILE = "/tmp/" . md5($BLOGURL); $CACHETIME = 0.5; # hours

You can see from the first line that this script accepts a single $_GET parameter $q defining the search string. If this variable is not set then the default search is for 'twitter'. The other parameters should be self-explanatory.

To do a search on Twitter we just send an HTTP request to their search script including the format (in this case 'rss') and the query string. Other more advanced options (see References below) are also available. For our purposes just the rpp (number of tweets to return per page) parameter is enough. No authentication is necessary as we're only reading public content, but it is rate limited.

For security it's a good idea to expand any 'shortened' urls in the results. That way people know what they're clicking on. We do this using curl and a crafty regular expression that detects most known url-shortening services:

# Original PHP code by Chirp Internet: www.chirp.com.au # Please acknowledge use of this code by including this header. function expandLinks(&$input) { # links matching the following regular expressions will be checked for redirects and expanded $domains = array( '[a-z0-9]{2,3}\.[a-z]{2}', '[a-z]{3,4}url\.com' ); if(preg_match_all("@http://((" . implode("|", $domains) . ")/[-a-z0-9]+)@i", $input, $matches)) { $matches = array_unique($matches[1]); foreach($matches as $shorturl) { $command = "curl --head " . escapeshellarg($shorturl) . " | awk '($1 ~ /^Location/){print $2}'"; if($expandedurl = exec($command)) { $input = str_replace("http://$shorturl", htmlspecialchars($expandedurl), $input); } } } }

We could also just try to expand every URL detected, but then you're making a lot of unnecessary HEAD requests for those links that pass through Twitter unshortened.

Important: Using the PHP Curl library is also possible and might be necessary if you're using Windows. The script presented above requires command-line curl access and the awk function. You can find a script that uses PHP Curl under References below.

Now we need a way to download and store (cache) the feed contents. We do this using a simple function:

function updateFeed() { global $BLOGURL, $CACHEFILE; ini_set('user_agent', "TheArtOfWeb (http://{$_SERVER['HTTP_HOST']})"); if($feed_contents = file_get_contents($BLOGURL)) { # expand shortened urls expandLinks($feed_contents); # write feed contents to cache file $fp = fopen($CACHEFILE, 'w'); fwrite($fp, $feed_contents); fclose($fp); } }

The updateFeed function when called simply downloads and stores the raw RSS file contents. Note that before making the API request to Twitter we are setting the user agent. You don't have to set a user agent, but if you don't your requests can be rate-limited.

Displaying the search results in HTML format

In the next section we display a short paragraph introducing the search results. Both the search string and the last modified date of the cached file are displayed:

echo "<p>Read the latest tweets mentioning <b>$q</b> as of "; if(file_exists($CACHEFILE)) { echo date('g:ia', filemtime($CACHEFILE)) . ' local time'; } else { echo 'right now'; } echo ":</p>\n\n";

If no cached file exists for this query the updateFeed function is called to fetch it for the first time.

# download the feed iff cached version is missing if(!file_exists($CACHEFILE)) updateFeed();

Finally we're ready to display the search results on the page. This section is almost identical to that introduced in previous articles, the only difference being that it's been customised specifically for Twitter content:

include "class.myrssparser.php"; $rss_parser = new myRSSParser($CACHEFILE); # read feed data from cache file $feeddata = $rss_parser->getRawOutput(); extract($feeddata['RSS']['CHANNEL'][0], EXTR_PREFIX_ALL, 'rss'); # display feed items if($rss_ITEM) { echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\">\n"; foreach($rss_ITEM as $itemdata) { preg_match("/^(.*)@twitter\.com \((.*)\)$/", $itemdata['AUTHOR'], $regs); list($foo, $author, $name) = $regs; echo "<tr>\n"; echo "<td><a title=\"$name\" href=\"http://twitter.com/$author\" target=\"_blank\"><img src=\"{$itemdata['GOOGLE:IMAGE_LINK']}\" width=\"48\" height=\"48\" border=\"0\" alt=\"$author\"></a></td>\n"; echo "<td><p><a href=\"http://twitter.com/$author\" target=\"_blank\">$author</a>: "; echo str_replace('<a ', '<a target="_blank" ', stripslashes($itemdata['DESCRIPTION'])); echo "<br>\n"; echo "<small>"; echo date($TIMEFORMAT, strtotime($itemdata['PUBDATE'])); echo " <a href=\"{$itemdata['GUID']}\" target=\"_blank\">View Tweet</a>"; echo "</small>"; echo "</p></td>\n"; echo "</tr>\n"; } echo "</table>\n\n"; } ?>

The output, as you can probably work out, will be a two-column HTML table displaying the users twitter icon, the update text and timestamp. We've also included some links where appropriate.

As a final step the following code should be placed right at the end of the page after all the HTML has been displayed:

<?PHP # download the feed iff cached version is too old if((time() - filemtime($CACHEFILE)) > 3600 * $CACHETIME) { flush(); updateFeed(); } ?>

The function of this code and the reason for placing it at the end of the page has been described in a previous article.

Sample output of Twitter search results

Here you can see the output of this script more or less exactly as presented. All we've done is added some smilies:

Read the latest tweets mentioning twitter as of 7:12am local time:

djqko

djqko: RT @shnskm: マツコ・デラックスが『Twitter』を始めるもフォローは27人!http://getnews.jp/archives/51037
11 March 2010, 7:11am

gresco

gresco: Are you seeing Twitter's new hovercards on your browser. I'm not. This is from a friend. http://twitpic.com/17u20i
11 March 2010, 7:11am

amandacmm

amandacmm: Sign up free and Expand your Twitter followers using http://FollowersTrain.com
11 March 2010, 7:11am

triplep220

triplep220: @MotorBug Woah, @workerofwood... Haven't seen that name on twitter for a long time...
11 March 2010, 7:11am

myfloridamarket

myfloridamarket: Google Finally Gets Real Time: Want to find out what people are saying right now at a conference? Twitter search.... http://www.econtentmag.com/Articles/Column/After-Thought/Google-Finally-Gets-Real-Time-61566.htm?utm_source=twitterfeed&utm_medium=twitter
11 March 2010, 7:11am

BeautflBlss421

BeautflBlss421: Tutu always be on some other shit when she be on twitter lmfaoooo
11 March 2010, 7:11am

t_rave

t_rave: Super Secret Link was just sent out. If you didn't get it, come find me on twitter or wait till the vlog goes live. http://www.facebook.com/permalink.php?story_fbid=361053529167&id=7752477274
11 March 2010, 7:11am

BaadHealthfreak

BaadHealthfreak: Look at this awesome twitter marketing video series. I watched the video and now I'm making money with my Twitter Acct! http://02705cu9dzw5xwhpza7lue47zx.hop.clickbank.net/
11 March 2010, 7:11am

grinding4ddub

grinding4ddub: hey everyone..... babyboy @jbr1985 is in twitter jail ...AGAIN..... but you can DM him
11 March 2010, 7:11am

karianned

karianned: @SaraNDesign Hi Sara, you are one of my newest followers! Love meeting new folks even if its on Twitter.
11 March 2010, 7:11am

This script is experimental so you use it at your own risk. Please send questions and comments via the Feedback link below.

Contents of the Twitter search RSS Feed

Here you can see exactly what kind of data we're receiving back from Twitter in RSS format:

<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:google="http://base.google.com/ns/1.0" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:twitter="http://api.twitter.com/">   <channel>     <title>twitter - Twitter Search</title>     <description>twitter - Twitter Search</description>     <link>http://search.twitter.com/search?q=twitter</link>     <twitter:refresh_url>http://search.twitter.com/search.rss?q=twitter&amp;rpp=10&amp;since_id=10310178002</twitter:refresh_url>     <pubDate>Thu, 11 Mar 2010 06:11:11 +0000</pubDate>     <openSearch:itemsPerPage>10</openSearch:itemsPerPage>     <item>       <title>RT @shnskm: &#12510;&#12484;&#12467;&#12539;&#12487;&#12521;&#12483;&#12463;&#12473;&#12364;&#12302;Twitter&#12303;&#12434;&#22987;&#12417;&#12427;&#12418;&#12501;&#12457;&#12525;&#12540;&#12399;27&#20154;&#65281; http://getnews.jp/archives/51037</title>       <link>http://twitter.com/djqko/statuses/10310178002</link>       <description>RT &lt;a href=&quot;http://twitter.com/shnskm&quot;&gt;@shnskm&lt;/a&gt;: &#12510;&#12484;&#12467;&#12539;&#12487;&#12521;&#12483;&#12463;&#12473;&#12364;&#12302;Twitter&#12303;&#12434;&#22987;&#12417;&#12427;&#12418;&#12501;&#12457;&#12525;&#12540;&#12399;27&#20154;&#65281; &lt;a href=&quot;http://getnews.jp/archives/51037&quot;&gt;http://getnews.jp/archives/51037&lt;/a&gt;</description>       <pubDate>Thu, 11 Mar 2010 06:11:11 +0000</pubDate>       <guid>http://twitter.com/djqko/statuses/10310178002</guid>       <author>djqko@twitter.com (DJ&#24613;&#34892;)</author>       <media:content url="http://a3.twimg.com/profile_images/640733599/billy--mandy-3302_normal.jpg" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a3.twimg.com/profile_images/640733599/billy--mandy-3302_normal.jpg</google:image_link>     </item>     <item>       <title>Are you seeing Twitter's new hovercards on your browser. I'm not. This is from a friend. http://twitpic.com/17u20i</title>       <link>http://twitter.com/gresco/statuses/10310177593</link>       <description>Are you seeing &lt;b&gt;Twitter&lt;/b&gt;&amp;apos;s new hovercards on your browser. I&amp;apos;m not. This is from a friend. &lt;a href=&quot;http://twitpic.com/17u20i&quot;&gt;http://twitpic.com/17u20i&lt;/a&gt;</description>       <pubDate>Thu, 11 Mar 2010 06:11:10 +0000</pubDate>       <guid>http://twitter.com/gresco/statuses/10310177593</guid>       <author>gresco@twitter.com (Gregg Scott)</author>       <media:content url="http://a3.twimg.com/profile_images/736691591/twitter_strutta_gresco_copy_normal.png" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a3.twimg.com/profile_images/736691591/twitter_strutta_gresco_copy_normal.png</google:image_link>     </item>     <item>       <title>Sign up free and Expand your Twitter followers using http://FollowersTrain.com</title>       <link>http://twitter.com/amandacmm/statuses/10310177459</link>       <description>Sign up free and Expand your &lt;b&gt;Twitter&lt;/b&gt; followers using &lt;a href=&quot;http://FollowersTrain.com&quot;&gt;http://FollowersTrain.com&lt;/a&gt;</description>       <pubDate>Thu, 11 Mar 2010 06:11:09 +0000</pubDate>       <guid>http://twitter.com/amandacmm/statuses/10310177459</guid>       <author>amandacmm@twitter.com (Amanda Campos)</author>       <media:content url="http://a3.twimg.com/profile_images/685909049/imagem_normal.JPG" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a3.twimg.com/profile_images/685909049/imagem_normal.JPG</google:image_link>     </item>     <item>       <title>@MotorBug Woah, @workerofwood... Haven't seen that name on twitter for a long time...</title>       <link>http://twitter.com/triplep220/statuses/10310177329</link>       <description>&lt;a href=&quot;http://twitter.com/MotorBug&quot;&gt;@MotorBug&lt;/a&gt; Woah, &lt;a href=&quot;http://twitter.com/workerofwood&quot;&gt;@workerofwood&lt;/a&gt;... Haven&amp;apos;t seen that name on &lt;b&gt;twitter&lt;/b&gt; for a long time...</description>       <pubDate>Thu, 11 Mar 2010 06:11:09 +0000</pubDate>       <guid>http://twitter.com/triplep220/statuses/10310177329</guid>       <author>triplep220@twitter.com (Jeff)</author>       <media:content url="http://a1.twimg.com/profile_images/662697818/IMG_7846_normal.JPG" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a1.twimg.com/profile_images/662697818/IMG_7846_normal.JPG</google:image_link>     </item>     <item>       <title>Google Finally Gets Real Time: Want to find out what people are saying right now at a conference? Twitter search.... http://www.econtentmag.com/Articles/Column/After-Thought/Google-Finally-Gets-Real-Time-61566.htm?utm_source=twitterfeed&amp;utm_medium=twitter</title>       <link>http://twitter.com/myfloridamarket/statuses/10310177116</link>       <description>Google Finally Gets Real Time: Want to find out what people are saying right now at a conference? &lt;b&gt;Twitter&lt;/b&gt; search.... &lt;a href=&quot;http://www.econtentmag.com/Articles/Column/After-Thought/Google-Finally-Gets-Real-Time-61566.htm?utm_source=twitterfeed&amp;utm_medium=twitter&quot;&gt;http://www.econtentmag.com/Articles/Column/After-Thought/Google-Finally-Gets-Real-Time-61566.htm?utm_source=twitterfeed&amp;utm_medium=twitter&lt;/a&gt;</description>       <pubDate>Thu, 11 Mar 2010 06:11:09 +0000</pubDate>       <guid>http://twitter.com/myfloridamarket/statuses/10310177116</guid>       <author>myfloridamarket@twitter.com (Glenn Fach)</author>       <media:content url="http://s.twimg.com/a/1267643725/images/default_profile_4_normal.png" type="image/jpg" width="48" height="48"/>       <google:image_link>http://s.twimg.com/a/1267643725/images/default_profile_4_normal.png</google:image_link>     </item>     <item>       <title>Tutu always be on some other shit when she be on twitter lmfaoooo</title>       <link>http://twitter.com/BeautflBlss421/statuses/10310176958</link>       <description>Tutu always be on some other shit when she be on &lt;b&gt;twitter&lt;/b&gt; lmfaoooo</description>       <pubDate>Thu, 11 Mar 2010 06:11:08 +0000</pubDate>       <guid>http://twitter.com/BeautflBlss421/statuses/10310176958</guid>       <author>BeautflBlss421@twitter.com (KayKay.)</author>       <media:content url="http://a3.twimg.com/profile_images/744510771/mail.google.com_normal.jpeg" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a3.twimg.com/profile_images/744510771/mail.google.com_normal.jpeg</google:image_link>     </item>     <item>       <title>Super Secret Link was just sent out. If you didn't get it, come find me on twitter or wait till the vlog goes live. http://www.facebook.com/permalink.php?story_fbid=361053529167&amp;id=7752477274</title>       <link>http://twitter.com/t_rave/statuses/10310176783</link>       <description>Super Secret Link was just sent out. If you didn&amp;apos;t get it, come find me on &lt;b&gt;twitter&lt;/b&gt; or wait till the vlog goes live. &lt;a href=&quot;http://www.facebook.com/permalink.php?story_fbid=361053529167&amp;id=7752477274&quot;&gt;http://www.facebook.com/permalink.php?story_fbid=361053529167&amp;id=7752477274&lt;/a&gt;</description>       <pubDate>Thu, 11 Mar 2010 06:11:08 +0000</pubDate>       <guid>http://twitter.com/t_rave/statuses/10310176783</guid>       <author>t_rave@twitter.com (T-Rave)</author>       <media:content url="http://a3.twimg.com/profile_images/628241871/b_w_normal.jpg" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a3.twimg.com/profile_images/628241871/b_w_normal.jpg</google:image_link>     </item>     <item>       <title>Look at this awesome twitter marketing video series. I watched the video and now I'm making money with my Twitter Acct! http://02705cu9dzw5xwhpza7lue47zx.hop.clickbank.net/</title>       <link>http://twitter.com/BaadHealthfreak/statuses/10310176663</link>       <description>Look at this awesome &lt;b&gt;twitter&lt;/b&gt; marketing video series. I watched the video and now I&amp;apos;m making money with my &lt;b&gt;Twitter&lt;/b&gt; Acct! &lt;a href=&quot;http://02705cu9dzw5xwhpza7lue47zx.hop.clickbank.net/&quot;&gt;http://02705cu9dzw5xwhpza7lue47zx.hop.clickbank.net/&lt;/a&gt;</description>       <pubDate>Thu, 11 Mar 2010 06:11:08 +0000</pubDate>       <guid>http://twitter.com/BaadHealthfreak/statuses/10310176663</guid>       <author>BaadHealthfreak@twitter.com (John Spicer)</author>       <media:content url="http://a3.twimg.com/profile_images/626027449/Health_Nut_normal.jpg" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a3.twimg.com/profile_images/626027449/Health_Nut_normal.jpg</google:image_link>     </item>     <item>       <title>hey everyone..... babyboy @jbr1985 is in twitter jail ...AGAIN.....  but you can DM him :)</title>       <link>http://twitter.com/grinding4ddub/statuses/10310176142</link>       <description>hey everyone..... babyboy &lt;a href=&quot;http://twitter.com/jbr1985&quot;&gt;@jbr1985&lt;/a&gt; is in &lt;b&gt;twitter&lt;/b&gt; jail ...AGAIN.....  but you can DM him :)</description>       <pubDate>Thu, 11 Mar 2010 06:11:07 +0000</pubDate>       <guid>http://twitter.com/grinding4ddub/statuses/10310176142</guid>       <author>grinding4ddub@twitter.com (jaime)</author>       <media:content url="http://a1.twimg.com/profile_images/731049736/150221_normal.jpg" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a1.twimg.com/profile_images/731049736/150221_normal.jpg</google:image_link>     </item>     <item>       <title>@SaraNDesign Hi Sara, you are one of my newest followers! Love meeting new folks even if its on Twitter.</title>       <link>http://twitter.com/karianned/statuses/10310175497</link>       <description>&lt;a href=&quot;http://twitter.com/SaraNDesign&quot;&gt;@SaraNDesign&lt;/a&gt; Hi Sara, you are one of my newest followers! Love meeting new folks even if its on &lt;b&gt;Twitter&lt;/b&gt;.</description>       <pubDate>Thu, 11 Mar 2010 06:11:05 +0000</pubDate>       <guid>http://twitter.com/karianned/statuses/10310175497</guid>       <author>karianned@twitter.com (KariAnne D)</author>       <media:content url="http://a1.twimg.com/profile_images/394164972/DSC09035_normal.JPG" type="image/jpg" width="48" height="48"/>       <google:image_link>http://a1.twimg.com/profile_images/394164972/DSC09035_normal.JPG</google:image_link>     </item>   </channel> </rss>

expand code box

Related Articles

References

< PHP


Bookmark and Share

[top]