Skip to content

Instantly share code, notes, and snippets.

@dphiffer
Created February 22, 2011 20:30
Show Gist options
  • Save dphiffer/839328 to your computer and use it in GitHub Desktop.
Save dphiffer/839328 to your computer and use it in GitHub Desktop.
Last 100 Twitter mentions in CSV format
<?php
// Twitter username
$user = 'MuseumModernArt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.json?q=$user&result_type=recent&rpp=100");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
header('Content-Disposition: attachment; filename="mentions.csv"');
header("Content-Type: text/csv; charset=utf-8");
$response = json_decode($json);
$out = fopen('php://output', 'w');
foreach ($response->results as $tweet) {
fputcsv($out, array(
$tweet->created_at,
$tweet->from_user,
preg_replace('/\s+/', ' ', $tweet->text)
));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment