Get Twitter feed and display information using SimpleXML

孤街浪徒 提交于 2019-12-06 14:17:43

问题


Based on the thread With PHP preg_match_all, get value of href, I'm trying to get some information from a twitter feed.

Here is the feed url (for testing purpose): Twitter feed

Here is my code:

function parse_feed($process) {
        $xml  = @simplexml_load_string($process);
        $findTweet = $xml['entry'];
    return $findTweet;
}

$username = 'tweet';
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=2";

$feed = file_get_contents($feed);
    //echo $feed;
print_r(parse_feed($feed));

I never used SimpleXML before or worked with XML.

Can someone help me please?


回答1:


this site might be a good start http://www.php.net/manual/en/simplexml.examples-basic.php




回答2:


Okay Founf it!, here is the solution for who is interested... Thanks to m1k3y02 for the docs!

     function parse_feed($process) {
        $xml = new SimpleXMLElement($process);
        $n=0;
        foreach($xml->entry as $entry) {
            $tweets[$n] = array($entry->published,$entry->content);
            $n++;
        }
        return $tweets;
    }

    $twitter_username = 'tweet';
    $twitter_entries = 5;
    $feed = "http://search.twitter.com/search.atom?q=from:" . $twitter_username . "&rpp=".$twitter_entries;

    $feed = file_get_contents($feed);
    $tweets = parse_feed($feed);
    $n=0;
    $n_t = count($tweets);
    while($n < $n_t) {
        echo "<div class=\"tweet\"><img src=\"img/tweet.png\" valign=\"absmiddle\" /> ";
        echo $tweets[$n][1][0];
        echo "</div>";
        echo "<div class=\"date\">".$tweets[$n][0][0]."</div>";
        $n++;
    }


来源:https://stackoverflow.com/questions/4589735/get-twitter-feed-and-display-information-using-simplexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!