Dear fellas,

I've been reading and trying to change to the new Twitter API for like three weeks now.. I gotta tell you, it is really confusing on how to use it..

I was running a website (search tweets using GeoLocation, Longitude and Latitude);

I've been trying to fix it with the new API but I'm failing!

If someone can help in this, will be appreciated, and he'll be credited in the source code.

Here is the old (was working) code of the search:

Twitter.php

<?php

/*
 * Description: Fetch tweets from within a certain radius of a certain location defined by 
 *              longitude and latitude and provide a link back to them.
 * Parameters:  
 *   $long: Longitude (e.g. '40.757929')
 *   $lat: Latitude (e.g. '-73.985506')
 *   $radius: Distance (e.g '25km')
 *   $numberOfTweets: Number of tweets to display (e.g. 2)
 */

function getGeoTwitterFeed($long, $lat, $radius, $numberOfTweets ) {

    //spacer - do not change
    $sp = '%2C';

    //generate rss link
    $link = 'http://search.twitter.com/search.rss?geocode=' . $lat . $sp . $long . $sp . $radius;

    //load the feed into an xml element
    $current_xml = new SimpleXMLElement($link, null, true);

    //begin output as unordered list
    //print '';

    //count how many tweets have been output
    $count = 0;

    //print out each twitter link
    foreach($current_xml->channel->item as $item) {

        //remove all php and html tags
        $item->title = strip_tags($item->title);
        $item->pubdate = strip_tags($item->pubDate);
        $google = $item->children("http://base.google.com/ns/1.0"); 
        $profileimg = $google->{"image_link"};
        $normalimg = str_replace("_normal","_reasonably_small",$profileimg);

        //check limit of tweets to output
        if($count <= $numberOfTweets) {

            //output link
            print '<section class="cf"><a href="'.$item->link.'" target="_blank"><img alt="" src="'.$normalimg.'"><p>'.$item->title.'<span></span><b>'.$item->pubdate.'</b></p></a></section>' ;

            //increment counter for number of tweets output
            $count++;
        }

    }

    //end unordered list
    //print '';

}
?>

results.php

<?php
 require_once 'twitter.php';
 $long = $_GET["long"];
 $lat = $_GET["lat"]; 
 $radius = $_GET["rad"]; 
 $numberOfTweets = $_GET["num"]-1;
 getGeoTwitterFeed($long, $lat, $radius, $numberOfTweets );
?>

Thanks in advance :)

anyone?

Hi,

late reply, but it can still be useful for people searching. At line 19 you wrote:

$link = 'http://search.twitter.com/search.rss?geocode=' . $lat . $sp . $long . $sp . $radius;

which it may be valid in API 1.0, but in Twitter API 1.1 the link changes to:

$link = "https://api.twitter.com/1.1/search/tweets.json?geocode=$lat,$long,$radius&q=pizza";

The q parameter is required, so it cannot be omitted. Also you have to append mi (mile) or km (kilometer) to the radius variable.

More information: https://dev.twitter.com/docs/api/1.1/get/search/tweets

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.