NiftyHost Forums (Archive)
Yahoo! Query Language (YQL) - Printable Version

+- NiftyHost Forums (Archive) (https://niftyhost.chary.us/support)
+-- Forum: Technology (https://niftyhost.chary.us/support/forum-7.html)
+--- Forum: Coding (https://niftyhost.chary.us/support/forum-12.html)
+--- Thread: Yahoo! Query Language (YQL) (/thread-1071.html)



Yahoo! Query Language (YQL) - Zach - 04-07-2011

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.


RE: Yahoo! Query Language (YQL) - HiddenKnowledge - 04-07-2011

Thanks, I'll be using that. Would love a tutorial or some proper/easy examples :)


RE: Yahoo! Query Language (YQL) - Zach - 04-07-2011

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>';
?>



RE: Yahoo! Query Language (YQL) - HiddenKnowledge - 04-07-2011

Thanks :)


RE: Yahoo! Query Language (YQL) - JonattanD - 04-09-2011

Awesom i dint knew you can access facebook via YQL


RE: Yahoo! Query Language (YQL) - Zach - 04-09-2011

Facebook, YouTube, and a whole lot more.


RE: Yahoo! Query Language (YQL) - JonattanD - 04-11-2011

can you also access MTV via it or any other music sites?


RE: Yahoo! Query Language (YQL) - Zach - 04-16-2011

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.