Skip to content

Instantly share code, notes, and snippets.

@iwek
Created July 9, 2012 17:09
Show Gist options
  • Save iwek/3077678 to your computer and use it in GitHub Desktop.
Save iwek/3077678 to your computer and use it in GitHub Desktop.
Twitter List Tweets PHP script that order tweets by user.
<?php
/**
* Twitter List Tweets PHP script that order tweets by user.
*
* @author http://techslides.com
* @link http://techslides.com/grab-twitter-list-tweets-order-by-user
*/
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
$json_output = json_decode($result,true);
curl_close($ch);
return $json_output;
}
function autolink($str, $attributes=array()) {
$attrs = '';
foreach ($attributes as $attribute => $value) {
$attrs .= " {$attribute}=\"{$value}\"";
}
$str = ' ' . $str;
$str = preg_replace(
'`([^"=\'>])((http|https|ftp)://[^\s<]+[^\s<\.)])`i',
'$1<a href="$2"'.$attrs.'>$2</a>',
$str
);
$str = substr($str, 1);
return $str;
}
function getResults($json){
$results = array();
foreach($json as $e){
$user = $e['user'];
//clean up wierd characters
$clean = preg_replace('/[^(\x20-\x7F)\x0A]*/','', $e['text']);
$text = autolink($clean);
$final = '<div class="item"><a href="http://twitter.com/'.$user["screen_name"].'"><img src="'.$user["profile_image_url"].'" alt="'.$user["name"].'" /></a><div class="tweet">'.$text.'</div></div>';
$key = $user["id"];
$results[$key][] = $final;
}
return $results;
}
$url = 'http://api.twitter.com/1/lists/statuses.json?slug=most-influential-in-tech&owner_screen_name=Scobleizer&per_page=100';
$json = curl($url);
$clean = getResults($json);
foreach ($clean as $a) {
foreach ($a as $b) {
echo "<li>".$b."</li>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment