Skip to content

Instantly share code, notes, and snippets.

@sudar
Created October 9, 2010 12:53
Show Gist options
  • Save sudar/618161 to your computer and use it in GitHub Desktop.
Save sudar/618161 to your computer and use it in GitHub Desktop.
Code samples to access YQL. More details at http://sudarmuthu.com/blog/code-sample-to-access-yql
<html>
<head>
<title>YUI in YQL</title>
<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>
<script type="text/javascript">
YUI().use('node', 'yql', function(Y) {
Y.YQL('SELECT * FROM upcoming.events WHERE location = "Bangalore"', function(r) {
// process the result json object
console.log(r.query.results.event);
});
});
</script>
</head>
<body>
</body>
</html>
<?php
//code to access YQL inside an YAP app
require('path/to/lib/Yahoo.inc'); //include Yahoo social SDK
define('CONSUMER_KEY', 'Your consumer key');
define('CONSUMER_SECRET', 'your consumer secret');
$yql_query = "SELECT * FROM upcoming.events WHERE location = '%s'"; //YQL query to retrieve search results
$value = "bangalore";
$app = new YahooApplication(CONSUMER_KEY, CONSUMER_SECRET);
$queryResponse = $app->query(sprintf($yql_query, $value));
var_dump($queryResponse);
?>
<?php
//Code to access YQL using PHP
$yql_query = "SELECT * FROM upcoming.events WHERE location = '%s'"; //YQL query to retrieve search results
$value = "bangalore";
var_dump(getResultFromYQL(sprintf($yql_query, $value)));
/**
* Function to get results from YQL
*
* @param String $yql_query - The YQL Query
* @param String $env - Environment in which the YQL Query should be executed. (Optional)
*
* @return object response
*/
function getResultFromYQL($yql_query, $env = '') {
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json";
if ($env != '') {
$yql_query_url .= '&env=' . urlencode($env);
}
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//Uncomment if you are behind a proxy
//curl_setopt($session, CURLOPT_PROXY, 'Your proxy url');
//curl_setopt($session, CURLOPT_PROXYPORT, 'Your proxy port');
//curl_setopt($session, CURLOPT_PROXYUSERPWD, 'Your proxy password');
$json = curl_exec($session);
curl_close($session);
return json_decode($json);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment