Skip to content

Instantly share code, notes, and snippets.

@jdp
Created January 19, 2009 23:38
Show Gist options
  • Save jdp/49236 to your computer and use it in GitHub Desktop.
Save jdp/49236 to your computer and use it in GitHub Desktop.
<?php
define('DEBUG', true);
// configure curl for twitter
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// example search for #chirp hashtag with bit.ly from dan
$api_url = 'http://search.twitter.com/search.json?q=%23chirp+bit.ly&from=hanggliding';
curl_setopt($ch, CURLOPT_URL, $api_url);
$results = curl_exec($ch);
curl_close($ch);
$results = json_decode($results);
// show each bit.ly url title
$bitly_urls = array();
foreach($results->results as $result) {
preg_match('/http:\/\/bit\.ly\/([a-z0-9]{4})/i', $result->text, $matches);
array_push($bitly_urls, array('short' => $matches[0], 'hash' => $matches[1]));
}
// configure curl for bitly
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, 'magicquotes:gayjustin69');
// get html title for each url
foreach($bitly_urls as &$bitly_url) {
$api_url = sprintf('http://api.bit.ly/info?version=2.0.1&shortUrl=%s', urlencode($bitly_url['short']));
curl_setopt($ch, CURLOPT_URL, $api_url);
$bitly_url_info = json_decode(curl_exec($ch));
if ($bitly_url_info->error_code == 0) {
$bitly_url['long'] = $bitly_url_info->results->{$bitly_url['hash']}->longUrl;
$bitly_url['title'] = $bitly_url_info->results->{$bitly_url['hash']}->htmlTitle;
}
}
curl_close($ch);
// output our aggregated data
echo '<pre>';
print_r($bitly_urls);
echo '</pre>';
?>
Array
(
[0] => Array
(
[short] => http://bit.ly/mhX8
[hash] => mhX8
[long] => http://7.media.collegehumor.com/collegehumor/ch6/c/3/collegehumor.1703899c3fe4ada5da7fb78378b86c9a.jpg
[title] =>
)
[1] => Array
(
[short] => http://bit.ly/6xPV
[hash] => 6xPV
[long] => http://www.textfiles.com/fun/plane.txt
[title] =>
)
[2] => Array
(
[short] => http://bit.ly/3SCO
[hash] => 3SCO
[long] => http://fixedframe.com/alex-trebek.gif
[title] =>
)
[3] => Array
(
[short] => http://bit.ly/2YD7
[hash] => 2YD7
[long] => http://www.addictinggames.com/skirunner.html
[title] =>
)
[4] => Array
(
[short] => http://bit.ly/GcJw
[hash] => GcJw
[long] => http://en.wikipedia.org/wiki/Hotel_For_Dogs
[title] => Hotel for Dogs (film) - Wikipedia, the free encyclopedia
)
[5] => Array
(
[short] => http://bit.ly/vUF6
[hash] => vUF6
[long] => http://chairs.com/
[title] => Chairs.com
)
[6] => Array
(
[short] => http://bit.ly/UUL6
[hash] => UUL6
[long] => https://twitter.com/hanggliding
[title] =>
)
[7] => Array
(
[short] => http://bit.ly/qJ1p
[hash] => qJ1p
[long] => http://undefined/
[title] =>
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment