Php code to Fetch Twitter Feeds

Php code to fetch Twitter feeds

Php code to fetch Twitter feeds

Following php code will help you to fetch twitter feeds from your account. No authentication is needed for this. Perhaps it can be used to display twitter feeds in forums, webpages etc. CURL should be activated for working of the code.  Please find the php code below.

$curl = curl_init();
// The twitter-id should be your twitter username example: sarathdr
curl_setopt ($curl, CURLOPT_URL, "http://search.twitter.com/search.atom?q=<twitter-d>&amp;amp;amp;rpp=5");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
$result = curl_exec ($curl);
curl_close ($curl);
$tweets = new SimpleXMLElement($result);
 
?>
 
<?php if( isset( $tweets ) && isset( $tweets ) && count($tweets->entry) > 0  ): ?>
 
    <ul>
        <?php $i=0; foreach($tweets->entry as $entry ): ?>
		<li><?=preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $entry->title);?></li>
        <?php endforeach; ?>
    </ul>

Leave a Reply

Copyright © 2012 Mesmerizing Moments | sarathdr.com.