NiftyHost Forums (Archive)

Full Version: Yahoo! Query Language (YQL)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I thought this was great. Yahoo! Query Language lets you access many web APIs using a (mostly) standardized SQL-like language. Currently, you can access:
  • Yahoo! Search Results
  • Facebook
  • Twitter
  • GeoIP
  • and a bunch more.
It's really easy to use, too. You can get results in JSON or XML, whichever you prefer to work with, via a simple file_get_contents() in PHP or a similar function in any other language.
Recently, I used it for the "Recent Forum Posts" and "Latest News" sections on the new NiftyHost.us website. YQL has a 'feed' table that parses RSS and Atom feeds for you quickly and easily. This allowed me to set up those sections in a matter of minutes.
Thanks, I'll be using that. Would love a tutorial or some proper/easy examples :)
Well, here's the code I use in my functions files to make it work in the first place:
PHP Code:
<?php
    
function yql_query($q) {
        
// simple wrapper function to send a YQL query,
        // get the results, and return it as a raw array.
        
$q objectToArray(json_decode(file_get_contents('http://query.yahooapis.com/v1/public/yql?format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q=' urlencode($q))));
        return 
$q;
    }
    function 
objectToArray($d) {
        if (
is_object($d)) {
            
// Gets the properties of the given object
            // with get_object_vars function
            
$d get_object_vars($d);
        }
 
        if (
is_array($d)) {
            
/*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            
return array_map(__FUNCTION__$d);
        }
        else {
            
// Return array
            
return $d;
        }
    }
?>
Then, we can use yql_query($query) to submit a query and get the results and stuff as an array.
To experiment with YQL and discover new tables, use the YQL Console at http://developer.yahoo.com/yql/console/. You can also use that to find the structure of the results which yql_query would return from each table.
Here's an example of using YQL to get the latest news from the NiftyHost Forums and to display it as an unordered list:
PHP Code:
<?php
echo '<ul>';
$latest yql_query('select * from feed where url="http://www.niftyhost.us/support/syndication.php?fid=4&limit=3"');
foreach (
$latest['query']['results']['item'] as $tidbit) {
echo 
'<li><a href="' $tidbit['link'] . '">' $tidbit['title'] . '</a><br>
<span class="small">' 
date('F j, Y, g:i a',strtotime($tidbit['pubDate'])) . '</span></li>';
}
echo 
'</ul>';
?>
Thanks :)
Awesom i dint knew you can access facebook via YQL
Facebook, YouTube, and a whole lot more.
can you also access MTV via it or any other music sites?
The only music thing it can access at this point in time appears to be Yahoo! Music. You can look around in the console and play with tables.