This post was written 3 years ago and therefore may not be as accurate as more recent posts.

This code pulls a json of a hashtag search from twitter. It runs some functions to add to a database. (functions not included.)

[php]
global $total, $hashtag;

$hashtag = ‘#somehashtag';
$total = 0;

function getTweets($hash_tag, $page) {
global $total, $hashtag;
$url = ‘http://search.twitter.com/search.json?q=’.urlencode($hash_tag).’&';
$url .= ‘page=’.$page;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($ch);
curl_close ($ch);

$json_decode = json_decode($json);

$json_output = json_decode($json,true);

for($c=0;$c<=count($json_decode->results);$c++){
if(isset($json_output[‘results’][$c][‘id_str’])){
if(!tweet_exists($json_output[‘results’][$c][‘id_str’])){
add_tweet($json_output[‘results’][$c][‘id_str’],
$json_output[‘results’][$c][‘from_user’],
$json_output[‘results’][$c][‘created_at’],
$json_output[‘results’][$c][‘profile_image_url’],
$json_output[‘results’][$c][‘text’]);
}
}
}

if($json_decode->next_page){
$temp = explode("&",$json_decode->next_page);
$p = explode("=",$temp[0]);
getTweets($hashtag,$p[1]);
}
}
[/php]

Categories: PHP, Twitter Dev

Tags: , ,
Shane Jones

Posted by

Shane is the Co-Founder of Peadig, the WordPress framework and has gained over 40,000 downloads of his WordPress plugins. Shane also likes to tinker with API's and specialises in Facebook, Twitter, Instagram and Foursquare API. The views expressed in Shane's posts are his own and are not necessarily the views of his employers.

Comments