I thought of adding code snippets for in different languages to access YQL. If your favorite language is not there then add a comment with the code snippet and I will add them as well.
JavaScript (using YUI)
<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
<?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);
}
?>
PHP (in YAP)
<?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);
?>
Dart
Find out how you can use YQL in Dart
Arduino
Yes, you can access YQL from Arduino and in fact you can also parse JSON in Arduino as well.
Leave a Reply