Skip to content

Instantly share code, notes, and snippets.

@rowanmanning
Created May 12, 2010 09:40
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 rowanmanning/398388 to your computer and use it in GitHub Desktop.
Save rowanmanning/398388 to your computer and use it in GitHub Desktop.
get_feed php functions
<?php
/**
* Get an RSS or ATOM feed.
* @param string $url The URL of the feed to get.
* @param integer $count [optional] The number of entries to get. Default value is 10.
* @param string $api_key [optional] The Google AJAX Feed API key. Default value is NULL.
* @param string $language [optional] The language to retrieve the feed in. Default value is NULL.
* @return array Returns an array representing the feed.
*/
function get_feed($url, $count=10, $api_key=null, $language=null){
// sort query parameters
$params = array(
'v' => '1.0',
'q' => $url,
'num' => (int)$count,
'format' => 'json'
);
if ($api_key){
$params['key'] = $api_key;
}
if ($language){
$params['hl'] = $language;
}
// get feed URL
$feed_url = 'http://ajax.googleapis.com/ajax/services/feed/load?' . http_build_query($params);
// get feed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
// decode feed
$feed = json_decode($feed, true);
// return feed as array
return $feed;
}
/**
* Get a user's Google Buzz feed.
* @param string $username The Google username or user ID to get a feed from.
* @param integer $count [optional] The number of buzz to get. Default value is 10.
* @param string $api_key [optional] The Google AJAX Feed API key. Default value is NULL.
* @param string $language [optional] The language to retrieve the feed in. Default value is NULL.
* @return array Returns an array representing the user's Google Buzz feed.
*/
function get_google_buzz_feed($username, $count=10, $api_key=null, $language=null){
// get feed URL
$feed_url = 'http://buzz.googleapis.com/feeds/' . $username . '/public/posted';
// return feed as array
return get_feed($feed_url, $count, $api_key, $language);
}
/**
* Get a user's Twitter feed.
* @param string $username The Twitter username to get a feed from.
* @param integer $count [optional] The number of tweets to get. Default value is 10.
* @return array Returns an array representing the user's Twitter feed.
*/
function get_twitter_feed($username, $count=10){
// sort query parameters
$params = array(
'count' => (int)$count
);
// get feed URL
$feed_url = 'http://twitter.com/statuses/user_timeline/' . $username . '.json?' . http_build_query($params);
// get feed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
// decode feed
$feed = json_decode($feed, true);
// return feed as array
return $feed;
}
/**
* Get a user's Facebook feed.
* @param string $username The Facebook username or user ID to get a feed from (this can also be the name/id of a page).
* @param integer $count [optional] The number of status messages to get. Default value is 10.
* @return array Returns an array representing the user's Facebook feed.
*/
function get_facebook_feed($username, $count=10){
// sort query parameters
$params = array(
'limit' => (int)$count
);
// get feed URL
$feed_url = 'http://graph.facebook.com/' . $username . '/feed?' . http_build_query($params);
// get feed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
// decode feed
$feed = json_decode($feed, true);
// return feed as array
return $feed;
}
/**
* Get a user's Flickr feed.
* @param string $user_id The Flickr user ID to get a feed from.
* @param string $language [optional] The language to retrieve the feed in. Default value is NULL.
* @return array Returns an array representing the user's Flickr feed.
*/
function get_flickr_feed($user_id, $language=null){
// sort query parameters
$params = array(
'id' => $user_id,
'format' => 'php_serial'
);
if ($language){
$params['lang'] = $language;
}
// get feed URL
$feed_url = 'http://api.flickr.com/services/feeds/photos_public.gne?' . http_build_query($params);
// get feed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
// unserialize feed
$feed = unserialize($feed);
// return feed as array
return $feed;
}
/**
* Get a user's GitHub feed.
* @param string $username The GitHub username to get a feed from.
* @return array Returns an array representing the user's GitHub feed.
*/
function get_github_feed($username){
// get feed URL
$feed_url = 'http://github.com/' . $username . '.json';
// get feed
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
// decode feed
$feed = json_decode($feed, true);
// return feed as array
return $feed;
}
// testing
print '<h3>Regular Feed (BBC News)</h3>';
print '<pre>' . print_r(get_feed('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml', 3), true) . '</pre>';
print '<h3>Google Buzz Feed</h3>';
print '<pre>' . print_r(get_google_buzz_feed('rowanmanning', 3), true) . '</pre>';
print '<h3>Twitter Feed</h3>';
print '<pre>' . print_r(get_twitter_feed('rowanmanning', 3), true) . '</pre>';
print '<h3>Facebook Feed</h3>';
print '<pre>' . print_r(get_facebook_feed('cocacola', 3), true) . '</pre>';
print '<h3>GitHub Feed</h3>';
print '<pre>' . print_r(get_github_feed('rowanmanning'), true) . '</pre>';
print '<h3>Flickr Feed</h3>';
print '<pre>' . print_r(get_flickr_feed('49512158@N00'), true) . '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment