Developing with Yahoo! Pipes

Philip S Tellis

Yahoo! Inc

PHP Quebec 2009

$ whoami

flickr:code_martial/1543735477

Do you like Unix Pipes?

Do you deal with web services?

Have you ever wished that you could pass the output of multiple web services through a unix pipe?

VoilĂ : pipes.yahoo.com

VoilĂ : pipes.yahoo.com
(You need a Yahoo! Id)

Nomenclature

flickr:jurvetson/855530042

Part I: Creating Pipes using the UI

1A simple feed aggregator

1A simple feed aggregator

Let Pipes figure out the feed URL

1A simple feed aggregator

Let Pipes figure out the feed URL
(use the Fetch Site Feed source)

1A simple feed aggregator

limit it to 5 items only

1A simple feed aggregator

limit it to 5 items only
(use Truncate)

1A simple feed aggregator

Limit it to only 2 items per feed

2Fetch photos from flickr

2Fetch photos from flickr - configurable

2Fetch photos from flickr - url configurable

3Loops

For each item in a feed, do something

3Loops

Translate an English feed to French

3Loops

3Loops

How about doing it for title and description?

3Loops

4Subpipes

5BBC news in flickr

5BBC news in flickr

flickr:xiffy/3144579667_63ff470102

Can we do this with a subpipe?

6Flickr photos for a user

6Flickr photos for a user

What if we only know the username and not the nsid?

6Flickr photos for a user

6Flickr photos for a user

Pipes in the wild

flickr:moriza/96724309

Part II: Reading pipes in PHP

1How do you read in a URL in PHP?

1How do you read in a URL in PHP?

   $url = 'http://pipes.yahoo.com/pipes/pipe.run?'
        . '_id=vI7LxsJe3BGRHaLP6UjTQA&_render=php';

   function fetchURL($url)
   {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_POST, 0 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
   }

1How do you read in a URL in PHP?

   $url = 'http://pipes.yahoo.com/pipes/pipe.run?'
        . '_id=vI7LxsJe3BGRHaLP6UjTQA&_render=php';

   function fetchURL($url)
   {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_POST, 0 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
   }

1How do you read in a URL in PHP?

   $url = 'http://pipes.yahoo.com/pipes/pipe.run?'
        . '_id=vI7LxsJe3BGRHaLP6UjTQA&_render=php';

   function fetchURL($url)
   {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_POST, 0 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
   }

   $data = unserialize(fetchURL($url));

Part III: YQL

What is YQL?

What is YQL?

flickr:jeff_r_moss/2464431247_3f7fbdd313

What is YQL?

flickr:jeff_r_moss/2464431247_3f7fbdd313

What is YQL?

flickr:jeff_r_moss/2464431247_3f7fbdd313

What is YQL?

flickr:jeff_r_moss/2464431247_3f7fbdd313

YQL

Fetch flickr photos using YQL


<?php
$url 
"http://query.yahooapis.com/v1/public/yql?q=";
$q   "select * from flickr.photos.search(100) where text='phpquebec'";
$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";
}
?>

Flickr photos with YQL

Non-Yahoo! web services


<?php
$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='Montreal' and direct_flights_min=20";

$x simplexml_load_file($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;
}
?> 

Airports near Montréal

Merci

Made with Eric A Meyer's S5