PHP and Web 2.0 |
|
2024-11-26 |
|
|
12 |
|
|
SOAP makes my head hurt. Let's look at some stuff we can all actually understand instead.
Take a pinch of RSS
<?php
$url = 'http://buzz.yahoo.com/feeds/buzzoverl.xml';
$xml = @simplexml_load_file($url);
if($xml) {
foreach($xml->channel->item as $item) {
$ret[(string)$item->title] = (string)$item->link;
}
echo "<pre>"; print_r($ret); echo "</pre>";
} else echo "Can't get to Yahoo!";
?>
Output
Add a spoonful of REST
<?php
$srv='http://api.search.yahoo.com/ImageSearchService/V1/imageSearch';
foreach($ret as $key=>$link) {
$url = $srv . "?query=$key&appid=RESTDemo";
$obj = simplexml_load_file($url);
}
?>
A thimble of gradeschool math
And we end up with something like this
By the way, throwing a cache layer between you and whatever remote service you are
accessing tends to be a good idea. In our case we can do it like this:
<?php
$tmp = '/tmp/'.md5($q);
if(!file_exists($tmp) || filemtime($tmp) < (time()-7200)) {
$stream = fopen($url,'r');
$tmpf = tempnam('/tmp','YWS');
file_put_contents($tmpf, $stream);
fclose($stream);
rename($tmpf, $tmp);
}
$obj = simplexml_load_file($tmp);
?>