Skip to content

Instantly share code, notes, and snippets.

@Darkflib
Created October 2, 2012 07:20
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 Darkflib/3817012 to your computer and use it in GitHub Desktop.
Save Darkflib/3817012 to your computer and use it in GitHub Desktop.
Twitter to syslog streamer
<?php
/*
This has been used for testing a logstash install and searching infrastructure. It is a quick and dirty implementation of the twitter streaming api. A better way would be to do it with curl with callback handlers. Normal curl wont work as the stream doesn't have a proper end.
*/
set_time_limit(0);
$query_data = array('track' => '#devops', 'stall_warnings' => 'true');
$user = 'twitteraccount'; // replace with your account
$pass = 'twitterpassword'; // replace with your account
openlog("Twitstream", LOG_PID, LOG_LOCAL0);
$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
if(!$fp){
print "$errstr ($errno)\n";
} else {
$request = "GET /1/statuses/filter.json?" . http_build_query($query_data) . " HTTP/1.1\r\n";
$request .= "Host: stream.twitter.com\r\n";
$request .= "Authorization: Basic " . base64_encode($user . ':' . $pass) . "\r\n\r\n";
fwrite($fp, $request);
while(!feof($fp)){
$json = fgets($fp);
$data = json_decode($json, true);
if($data){
//
unset($data['entities']);
$data['user']='@'.$data['user']['screen_name'].', '.$data['user']['name'].' - '.$data['user']['location'];
print_r($data);
syslog(LOG_INFO,json_encode($data));
//
}
}
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment