Skip to content

Instantly share code, notes, and snippets.

@jdp
Created January 30, 2009 01:11
Show Gist options
  • Save jdp/54870 to your computer and use it in GitHub Desktop.
Save jdp/54870 to your computer and use it in GitHub Desktop.
<?php
/**
* Chirp 'people' API method
* @author Justin Poliey <justin@magicquotesgroup.com>
* @package chirp
* @version 1.0
*/
/**
* Require base API handler
*/
require '../core/apihandler.php';
/**
* Require Twitter Search component
*/
require '../core/twitter.php';
/**
* Require Bitly component
*/
require '../core/bitly.php';
/*
* Enforce API syntax
*/
if (!array_key_exists('hash', $_GET))
{
header('HTTP/1.1 400 Bad Request');
$error = array('error'=>'Missing hash parameter');
echo json_encode($error);
exit;
}
/*
* Perform a Twitter search for the #chirp hashtag and a Bitly URL specified
* in the GET 'hash' parameter. This automatically paginates, and returns a
* JSON object of all the instances of that Chirp'd URL.
*/
$search = new TwitterSearch();
$page_results = new stdClass();
$search_results = array();
$q = sprintf('http://bit.ly/%s', $_GET['hash']);
do
{
$query = (property_exists($page_results, 'next_page'))
? $page_results->next_page
: sprintf('?q=%s', urlencode($q));
$page_results = json_decode($search->rawSearch($query));
foreach($page_results->results as $page_result)
{
array_push($search_results, array(
'user' => $page_result->from_user,
'time' => $page_result->created_at,
'status' => $page_result->text
));
}
} while(property_exists($page_results, 'next_page'));
/*
* Set the status code and content-type, and then show the JSON
*/
header('HTTP/1.1 200 OK');
header('Content-type: text/plain');
echo json_encode($search_results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment