Yahoo! Pipes, PHP, and web services

September 1st, 2007

I’ve finally taken the time to play around with Yahoo Pipes. I’ll probably do a few posts over the next couple of days on my findings, but I’ll get started with the basics on how to write a json web service in PHP that can be called from Pipes. The capability to call a web service from Pipes is a recently added addition by Yahoo that could prove to be very powerful.

There’s a good post and recipe for getting started over at echoditto labs. Unfortunately, my web host had two things that made this recipe a non-starter. First, they don’t allow compiling C++ extensions for PHP, as is required by the JSON PECL package. Second, they have PHP’s annoying little magic_quotes_gpc setting turned on.

Here’s a little recipe that may help you get started. You’ll need to first download the Services_JSON library — a proposed addition to PEAR that encodes and decodes JSON. The recipe below assumes that you use items as the input for the “Path to item list” prompt in Pipes. The recipe is simple and straightforward. It is the moral equivalent of Yahoo’s Java example — it simply appends ” (This text was added by the external Web Service)” to the title of each item in the feed. It provides one additional bit of functionality that may help you get started. It logs the decoded input value to pipes-log.txt. This will help give you an idea of what your input is.

<?php
require_once 'JSON.php';

$json = new Services_JSON();

$value = $json->decode((get_magic_quotes_gpc() == 1) ? stripslashes($_POST['data']) : $_POST['data']);

$log_h = fopen("pipes-log.txt", "a");
fwrite($log_h, "Input Value: " . print_r($value, true) . "\n");
fclose($log_h);

for ($i=0; $iitems); $i++) {
  $value->items[$i]->title = $value->items[$i]->title . " (This text was added by the external Web Service)";
}

$output = $json->encode($value);

header('Content-Type: application/json');
print($output);
?>

Leave a Reply

Powered by WP Hashcash