Created
May 31, 2010 23:26
-
-
Save mrcgrtz/420381 to your computer and use it in GitHub Desktop.
My simple YQL helper function.
This file contains hidden or 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 | |
/** | |
* Get data via YQL | |
* | |
* @author Marc Görtz (http://marcgoertz.de/) | |
* @see https://gist.github.com/420381 | |
* @license http://creativecommons.org/licenses/by-sa/3.0/de/ | |
* @param string $select YQL query | |
* @param string $format output format, either 'json' or 'xml' (default: 'json') | |
* @param string $method HTTP method for accessing YQL (default: 'GET') | |
* @param mixed $cache maximum caching time in seconds or boolean FALSE (default: FALSE) | |
* @return object results as JSON or SimpleXML object | |
*/ | |
function yql($select, $format = 'json', $method = 'GET', $cache = FALSE) { | |
$yql = 'https://query.yahooapis.com/v1/public/yql?q=' . urlencode($select) . '&format=' . urlencode($format) . '&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&diagnostics=false'; | |
if ($cache) { | |
$yql .= '&_maxage=' . $cache; | |
} | |
$curlConnection = curl_init(); | |
curl_setopt($curlConnection, CURLOPT_URL, $yql); // URL to connect to | |
curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1); // return the result as string | |
curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE); // do not check SSL certificate (but use SSL of course), live dangerously! | |
switch (strtoupper($method)) { | |
case 'POST': | |
curl_setopt($curlConnection, CURLOPT_POST, 1); // use POST method | |
break; | |
case 'PUT': | |
curl_setopt($curlConnection, CURLOPT_PUT, 1); // use PUT method | |
break; | |
case 'GET': | |
default: | |
curl_setopt($curlConnection, CURLOPT_GET, 1); // use GET method | |
break; | |
} | |
$output = curl_exec($curlConnection); | |
curl_close($curlConnection); | |
$data = ($format === 'json') ? json_decode($output)->query : simplexml_load_string($output); | |
return $data->results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment