开发者

How to retrieve Twitter search results dynamically using Ajax?

开发者 https://www.devze.com 2023-03-17 07:41 出处:网络
I\'m building a Twitter application which depends on retrieving Twitter search results. It works fine but I still need to update the page to get the new tweets.

I'm building a Twitter application which depends on retrieving Twitter search results. It works fine but I still need to update the page to get the new tweets.

How to let the page refreshes itself dynamically just like Twitter Widgets, to show the new tweets ?

Here is my code, please show me how, because I tired many scripts and it doesn't work with me.

Twitter Class

<?php

class Twitter
{
    public function __construct(){  }

    public function searchResults( $search = null )
    {
        $url = "http://search.twitter.com/search.atom?q=" . urlencode( $search ) . "&lang=en&rpp=50";
        $curl开发者_如何学JAVA = curl_init();
        curl_setopt( $curl, CURLOPT_URL, $url );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
        $result = curl_exec( $curl );
        curl_close( $curl );

        $return = new SimpleXMLElement( $result );
        return $return;
        }
    }
?>

Test Class

<?php


require_once("twitter.class.php");
$Twitter = new Twitter;

$results = $Twitter->searchResults("usa");

foreach( $results->entry as $result )
{
    echo "<h3><a href=\"". $result->author->uri ."\">". $result->author->name ."<a/></h3><img src=\"". $result->link[1]->attributes()->href ."\" style=\"float: left;\"><p>". $result->content."</p><div style=\"clear:both;\">&nbsp;</div>";
}
?>

. .

Waiting for your response :)


You could poll for changes through javascript / ajax, by setting up a timer that calls a function in Test Class which returns a list of tweets. Example (using jquery because I am no javascript expert):

$(function() {

    function fetchTweets() {

        $.ajax({
          url: "testclass.php",
          success: function(data) {

            // Replace contents of one element with data
        }
    }

    setInterval(fetchTweets(), 60000);
}
0

精彩评论

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