Created
October 6, 2012 18:22
-
-
Save flotwig/3845707 to your computer and use it in GitHub Desktop.
A simple function to retrieve results from reddit's API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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