Skip to content

Instantly share code, notes, and snippets.

@skakri
Created November 17, 2012 15:49
Show Gist options
  • Save skakri/4096962 to your computer and use it in GitHub Desktop.
Save skakri/4096962 to your computer and use it in GitHub Desktop.
Twitter streaming API + URL unshortening
<?php
/**
* Unshorten links
*
* @param string $text
* @return string
*/
function unshorten_links($text) {
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$ch = curl_init($text);
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
CURLOPT_SSL_VERIFYPEER => FALSE,
));
curl_exec($ch);
$text = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $text;
');
return preg_replace_callback($pattern, $callback, $text);
}
set_time_limit(0);
$users = array(
'110089364' => array(
'nick' => 'Maadinsh',
'uid' => 1,
'gid' => 0
),
'172640737' => array(
'nick' => 'SkaForUs',
'uid' => 1548,
'gid' => 0
),
'123597407' => array(
'nick' => 'ieluvingrosana',
'uid' => 17077,
'gid' => 11
),
'932730655' => array(
'nick' => 'Rikinators',
'uid' => 22051,
'gid' => 0
),
'347040745' => array(
'nick' => 'StaticFake',
'uid' => 13004,
'gid' => 0
),
'65642258' => array(
'nick' => 'Trakais18',
'uid' => 1216,
'gid' => 0
),
'36635020' => array(
'nick' => 'viesty09',
'uid' => 2145,
'gid' => 0
),
'14276842' => array(
'nick' => 'skakri',
'uid' => 16261,
'gid' => 0
),
'104146775' => array(
'nick' => 'exs_lv',
'uid' => 1,
'gid' => 0
),
'15518000' => array() // freq tweeter test
);
function cgets($ch, $line) {
$length = strlen($line);
printf("Received %d byte\n", $length);
//echo($line);
$tweet = json_decode($line);
if (isset($tweet->{'text'})) {
//if ($tweet->{'retweet_count'} == 0 && !isset($tweet->{'in_reply_to_user_id'})) {
echo unshorten_links($tweet->{'text'}) . "\n";
//}
}
flush();
return $length;
}
while(true) {
$ch = curl_init();
echo http_build_query(
array(
'follow'=>implode(array_keys($users), ',')
)
, '', '&');
curl_setopt($ch, CURLOPT_URL, 'https://stream.twitter.com/1/statuses/filter.json');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'cgets');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 20000); // we want all tweet data in buffer, so json isn't malformed; we're not writing to a file
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
curl_setopt($ch, CURLOPT_HEADER, TRUE); // debug
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(
array(
'follow'=>implode(array_keys($users), ',')
)
, '', '&')
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000); // ~ 11 days
curl_exec($ch);
echo curl_getinfo($ch); // debug
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment