Yahoo! Query Language (YQL)
04-07-2011, 07:33 AM, (This post was last modified: 04-07-2011, 07:35 AM by Zach.)
#3
RE: Yahoo! Query Language (YQL)
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>';
?>
Hi! I'm Zach, and I founded NiftyHost. If you need anything, just PM me! :)
Reply


Messages In This Thread
Yahoo! Query Language (YQL) - by Zach - 04-07-2011, 06:50 AM
RE: Yahoo! Query Language (YQL) - by Zach - 04-07-2011, 07:33 AM
RE: Yahoo! Query Language (YQL) - by JonattanD - 04-09-2011, 04:13 AM
RE: Yahoo! Query Language (YQL) - by Zach - 04-09-2011, 10:10 AM
RE: Yahoo! Query Language (YQL) - by JonattanD - 04-11-2011, 06:39 AM
RE: Yahoo! Query Language (YQL) - by Zach - 04-16-2011, 10:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What coding language do you use? HiddenKnowledge 63 13,356 06-15-2012, 12:10 PM
Last Post: RichardGv

Forum Jump: