I didn’t even know Yahoo had released an API for their search until today, so I decided to mess around with it, anything but study accounting. Yahoo returns results as an XML file as opposed to Google with their SOAP. Wouldn’t it be so much easier if they used the same system as Google, we wouldn’t have to write new PHP scripts? Anyway, the PHP API example scripts supplied by Yahoo use the the document object model method of parsing XML in PHP which requires you to download and re-install PHP which I couldn’t be bothered doing. Also, a lot of people who don’t have a dedicated server might not be allowed to install this.
So I used the built-in XML parser functions that seems a messy method but it gets the job done. I went looking online for tutorials on parsing XML with PHP and found a great one on SitePoint by Kevin Yank. There’s no point in me copying that article. Read it. But the code provided didn’t work with the Yahoo XML because there’s two URL values, one the actual result URL and the other the Yahoo cache URL. So basically you just add a line of code in the startElement function (remember, you should have read the sitepoint article) that stops reading information when you reach the cache bit:
if ($tagName == "CACHE") {
$this->insideitem = false;
}
Here’s the full source of my adapted code that searches Yahoo for two Madonna results and then just spits out the two links.
Download Yahoo API PHP Script