开发者

PHP pick tweets code

开发者 https://www.devze.com 2023-02-25 09:26 出处:网络
I have a php code that fetches my latest tweet and displays it on my website. It works great but I would like to know if I could do this but maybe fetchin开发者_运维百科g my 5 last tweets instead of o

I have a php code that fetches my latest tweet and displays it on my website. It works great but I would like to know if I could do this but maybe fetchin开发者_运维百科g my 5 last tweets instead of only the latest. This is my code for twitter.php:

function returnTweet()
{

   $username = "username";
   $prefix = "<div><big><i><a href=\"http://twitter.com/$username\">@$username</a> ";
   $suffix = "</i></big></div>";

   $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=5";
   $twitterFeed = file_get_contents($feed);

   $tweet = parse_feed($twitterFeed);
   return $prefix.$tweet.$suffix;
} 


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);
    return $tweet;
}

And i display it in my php page like this:

include "twitter.php";              
echo "".returnTweet();

Would appreciate any help in this! Regards from Paparappa


You're much better off using an XML-parser for this (for example: http://www.php.net/manual/en/simplexml.examples-basic.php).

(not sure about the structure of twitter-atoms, so $xml->tweet[$i] is probably something else..)

$xml = new SimpleXMLElement($twitterFeed);
$tweets = array();
for ($i = 0; $i < 5; $i++) {
    $tweets[] = $xml->tweet[$i];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消