Get Twitter feed and display information using Sim

2019-04-16 01:03发布

问题:

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++;
    }