<slide title="Yahoo Query Language - YQL">

<break lines="1"/>
<blurb fontsize="4em">Combine all APIs on the Web into a single API!</blurb>
<link leader="Docs: " fontsize="3em" marginleft="1em">http://developer.yahoo.com/yql</link>
<link leader="Public URL: " fontsize="3em" marginleft="1em">http://query.yahooapis.com/v1/public/yql</link>
<link leader="OAuth'ed URL: " fontsize="3em" marginleft="1em">http://query.yahooapis.com/v1/yql</link>
<link leader="Cool Console: " fontsize="3em" marginleft="1em">http://developer.yahoo.com/yql/console/</link>

<example title="Public Flickr Search" result="1" fontsize="1.4em"><![CDATA[<?php
$url = "http://query.yahooapis.com/v1/public/yql?q=";
$q   = "select * from flickr.photos.search(100) where text='openhacknyc'";
$fmt = "xml";

$x = simplexml_load_file($url.urlencode($q)."&format=$fmt");

foreach($x->attributes('http://www.yahooapis.com/v1/base.rng') as $k=>$v) {
  $$k=(string)$v;
}

echo <<<EOB
 $count photos fetched from
 {$x->diagnostics->url} in 
 {$x->diagnostics->url['execution-time']} seconds<br>
EOB;

$flickr = "http://static.flickr.com/";
foreach($x->results->photo as $p) {
  echo "<img src=\"$flickr{$p['server']}/{$p['id']}_{$p['secret']}_s.jpg\"/>\n";
}
?>]]></example>

<example title="Etsy Listings Search" result="1" fontsize="1.4em"><![CDATA[<?php
include 'secrets.inc';
$url = "http://query.yahooapis.com/v1/public/yql?q=";
$q   = "select * from etsy.listings(100) 
        where color='AAAA22' and wiggle=8 and api_key='$etsy_api_key'";
$env = 'store://datatables.org/alltableswithkeys';
$fmt = "xml";

$x = simplexml_load_file($url.urlencode($q)."&format=$fmt&env=".urlencode($env));

foreach($x->results->results as $l) {
  echo <<<EOB
<a href="{$l->url}">
<img src="{$l->image_url_200x200}" title="{$l->listing_id}: {$l->title}">
</a>
EOB;
}
?>]]></example>

<example title="Add a Cache" result="1" fontsize="1.4em"><![CDATA[<?php
define('MINSZ',10);
function cache($url) {
  $tmp = '/tmp/'.md5($url);
  if(file_exists($tmp)) $st = stat($tmp);
  if(!$st || $st && ($st['size']<MINSZ || $st['mtime']<(time()-7200))) {
    if($st && $st['size']>=MINSZ) touch($tmp);  // Keep re-using entry
    $stream = fopen($url,'r');                  // until new is ready
    $tmpf = tempnam('/tmp','YWS');
    file_put_contents($tmpf, $stream);
    fclose($stream);
    rename($tmpf, $tmp);
  }
  return $tmp;
}
$yql = "http://query.yahooapis.com/v1/public/yql?q=";
$q = "use 'http://airports.pidgets.com/v1/tables.xml' as airports;".
     "select * from airports(5) where near=':-:location:-:' and direct_flights_min=20";

$x = simplexml_load_file(cache($yql.urlencode($q).'&format=xml'));
echo "<b>URLs accessed:</b><br>\n";
foreach($x->diagnostics->url as $u) {
  echo "<a href=\"$u\">$u</a><br>\n";
}
echo "<br>";
foreach($x->results->airport as $a) {
  echo <<<EOB
  {$a->code} <a href="{$a->url}">{$a->name}</a> {$a->dist}km<br>
EOB;
}
?>]]></example>

</slide>
