First steps within the Zend Framework

The “old” web search API from Yahoo is deprecated:
They discontinued the service in April 2011. For further details, please read the Deprecated Services blog post (there is a new API system).

Setup a quick Yahoo Search page within a few minutes!

A few days ago the “old” Google web search API has stopped working for the web public. Not a big surprise after the release of the AJAX web search API. A customer of mine is using search results in his mini sites to give them a little more body. This is why I needed to look for some alternative. During the Zend PHP conference earlier this year, I got in touch with the Zend framework and learned that there is already a class to obtain results from the Yahoo search engine. The class has features to search the Yahoo API for web sites, image, news and the local search results.

Installing the library

I decided some time before to use the classes from the Zend framework for future projects because the classes are well written in PHP5. If you like to do the same you should install the lib on a central place on your web server to get access to the framework from all your websites. After uploading the files I added this row to .htaccess file for the website I’m working on:

php_value include_path ".:/home/mylocation_for_zend_framework/"

This way you’re able to access the classes without to think about the class paths. Of course you are free to add this include path to the php.ini file or to your apache configuration.

Search Yahoo via the API

Before you can start you need to request a Yahoo developer key. Visit this page and get one for free.

The manual for the Zend framework has easy to use examples for all important methods. This way you’re able to have a result without to think about the code. Just create a file and add this code and add the Yahoo developer key:

require_once 'Zend/Service/Yahoo.php';
 
$yahoo = new Zend_Service_Yahoo("YAHOO_APPLICATION_ID");
$results = $yahoo->webSearch('PHP');
foreach ($results as $result) {
    echo $result->Title;
}

Run this script and you get the titles for the top10 results for a Yahoo search on “PHP”. Just enter a different search keyword (or variable) to get different results. According the Yahoo web search documentation we have some important search features like:

  • region (Yahoo has different local search engines for countries like UK, NL or DE)
  • results (the number of results you get back for your query: default 10, max. 100)
  • start (you need this one to get result pages like with a real search engine)
  • language (if you need other results than in English language)

There are more options check their site for more further reference. In our example we want to have control about the options above. To use them we need to add the keys and values into an array:

$options = array('start' => 10, 'language' => 'nl', 'results'  => 10, 'region' => 'nl');

This option array can be used in the “webSearch()” method this way:

$results = $yahoo->webSearch('PHP scripts', $options);

While executing this modified snippet we get some error because the option “region” is not included in the class. Not so bad, but it’s very important to have this option because the US version doesn’t have so much Dutch language results. After checking the class (file: Zend/Service/Yahoo.php) I found this code on row 529:

$validOptions = array('appid', 'query', 'results', 'start', 'language', 'type', 'format', 'adult_ok',
'similar_ok', 'country', 'site', 'subscription', 'license');

Just add the value “region” to this array and you’re fine (I will submit this change to Zend and I’m sure they will add this change to future releases).

If you combine this snipped with a form you have almost a full featured search box for your website. To create a complete result with summary and URL we need this code snippet (replace this code with the prev. “output” code):

echo '<p><a href="'.$result->Url.'">'.$result->Title.'</a><br />
'.$result->Summary.'</p>';

Thats all, only some small snippet and you have some fast Yahoo search results for your website.

Comments

  1. I’ve been meaning to check zend framework out. Will do it in the weekend.

    nice tutorial though.

  2. Hi Christian,

    sure this is not the only way to query the Yahoo API ;) the great thing is that doing this with the Zend framework is extremely easy even for beginners.

  3. if you are setting the include_path on a per-script basis, make sure you use PATH_SEPARATOR to let it run on all operating systems ;)
    Also, for simple Yahoo!, queries, a simplexml_load_file(‘http://–yahoo-api-url–/’) is probably even better.

  4. php_value include_path “.:/home/mylocation_for_zend_framework/”

    Just be aware this will not work on PHP as CGI – you’ll need to use set_include_path() in the bootstrap file if running PHP in this configuration. :)

  5. very help full, Great guide to begin learn zend framework. Thanks

  6. Hmm maybe I’ll have to learn how to use the Zend framework!
    Maybe I should get a bit better at PHP first though… ;-)

    Dom

  7. Very nice guide. You’ve interested me, going to look how many other PHP classes they have.

  8. Please post further questions via the forum:
    http://www.finalwebsites.com/forums/

Because of all the spam attemps I've decided to close the comment form at this time. If you have have any questions or comments please post them by using Google+ or Twitter (the links to my profiles are located at the top of this page).