Just wrote quick some code sample to show how to access YQL (Yahoo Query Language) queries from PHP.
You can use the following code sample to fetch results from YQL from any standard PHP file. You would require the curl extension to be installed. If you are behind a proxy, uncomment the 3 lines and replace it with proper proxy values
<?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);
}
?>
If you have the Yahoo social SDK installed and you want to access results from YQL, then you can use the following code sample. Replace the text consumer_key and consumer_secret with correct values.
<?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);
?>
Update : You can also get code snippets for other languages apart from PHP.
Related posts
PHP code sample to access YQL http://t.co/t7fBQxX
Code from @sudarmuthu PHP workshop at #hacku is available at http://t.co/aXFX0ubo and code to access YQL is at http://t.co/amCTDiX1 #iitm