Skip to content

Instantly share code, notes, and snippets.

@flotwig
Created October 6, 2012 18:22
Show Gist options
  • Save flotwig/3845707 to your computer and use it in GitHub Desktop.
Save flotwig/3845707 to your computer and use it in GitHub Desktop.
A simple function to retrieve results from reddit's API
<?php
// example usage:
// redditApi('user/flotwig'); will return an array with information about me
// redditApi('r/breakingbad'); will return an array of info about r/breakingbad
function redditApi($call,$domain='www.reddit.com') {
$url = 'http://' . $domain . '/' . $call . '.json';
if (function_exists('curl_init')) { // not all PHP installs have cURL
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_USERAGENT,'/u/flotwig'); // for stats
$result = curl_exec($ch);
} else {
$result = file_get_contents($url); // slower, but at least it'll still work
}
if (!$result) {
return FALSE; // something broke
}
$result = json_decode($result,TRUE);
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment