Skip to content

Instantly share code, notes, and snippets.

@yosun
Created August 28, 2012 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosun/3504505 to your computer and use it in GitHub Desktop.
Save yosun/3504505 to your computer and use it in GitHub Desktop.
<?php
/*// a quick'n'dirty php-only universal script
// that fetches the latest $total_pages*100 tweets
// containing the word $query
// coded live during #VMWorld during the #vHunt as an
// unofficial count of the number of tweets per participant
// hacked on an iPhone with ZAGG bluetooth keyboard
// using Textastic, which does not
// yet include code-formatting,
// so beautified using phpformatter :)
// usage:
// ?query=vhunt&sort=desc // for #vhunt descending order
// params:
// ?query=[insert search term] (mandatory)
// optional params:
// sort=desc for column-based view from descending order
// d=raw for print_r vardump
*/
if ($query != '') {
$total_pages = 10;
$blurb = array();
for ($i = 1; $i <= $total_pages; $i++) {
$blurb[$i] = file_get_contents('http://search.twitter.com/search.json?page=' . $i . '&rpp=100&q=' . $query);
}
$json = array();
for ($i = 1; $i <= $total_pages; $i++) {
$json[$i] = json_decode($blurb[$i]);
}
$responses = array();
for ($i = 1; $i <= $total_pages; $i++) {
$responses[$i] = $json[$i]->results;
}
$response = array();
for ($i = 1; $i <= $total_pages; $i++) {
$response = array_merge($response, $responses[$i]);
}
echo count($response) . 'results <p />';
$tweets = array();
for ($i = 0; $i < count($response); $i++) {
$userid = $response[$i]->from_user_id_str;
$tweets[$userid]['username'] = $response[$i]->from_user_name . ' @' . $response[$i]->from_user;
$tweets[$userid]['tweets'][] = $response[$i]->created_at . ' ' . $response[$i]->text;
$tweets[$userid]['count'] = $tweets[$userid]['count']++;
}
if ($d == 'raw') {
echo '<pre>';
//print_r($response);
print_r($tweets);
echo '</pre>';
} else {
if ($sort == 'desc') {
$tweetcount = array();
foreach ($tweets as $key => $val) {
$tweetcount[$key] = $tweets[$key]['tweets'];
}
array_multisort($tweetcount, SORT_DESC, $tweets);
echo '<table>';
echo '<tr>';
foreach ($tweets as $key => $val) {
echo '<td valign="top">';
echo '<h1>' . $tweets[$key]['username'] . '</h1><ol>';
for ($i = 0; $i < count($tweets[$key]['tweets']); $i++) {
echo '<li>' . $tweets[$key]['tweets'][$i] . '</li>';
}
echo '</td>';
}
echo '</tr>';
echo '</table>';
} else {
foreach ($tweets as $key => $val) {
echo '<hr /><p />';
echo '<h1>' . $tweets[$key]['username'] . '</h1><ol>';
for ($i = 0; $i < count($tweets[$key]['tweets']); $i++) {
echo '<li>' . $tweets[$key]['tweets'][$i] . '</li>';
}
echo '</ol>';
}
}
}
} else {
?>
<form action="parse.php">
<input type="text" name="query" />
<input type="submit" label="submit" />
</form>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment