dongwo8827523 2011-04-13 10:02
浏览 22
已采纳

PHP挑选推文代码

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 fetching 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

  • 写回答

1条回答 默认 最新

  • dongxizhe9755 2011-04-13 10:09
    关注

    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];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?