Primary links
Creating search engines using webservices
admin — Thu, 23/11/2006 - 11:49am
You must have seen many sites have a site search facility. You probably might have create many similiar sites too. Ok. But now you want an Internet Search Facility in your site. So what would you do? Quit jobs and join Stanford University to learn more about Data Mining Methodologies? Or start working on a million dollar prototype of (yet another) 'Jerry's Guide to the World Wide Web'?
Relax. There are easier ways to have a Search Engine in your site.
In this article we will look at how we can use Yahoo Web Services can help us have a SE up and running in a matter of minutes.
For starters, we are assuming that you have some idea of Web Services. If not, dont sweat. By the time you complete this article you will have a good idea of what web services are and a better idea of what they can do for you. To make it more interesting, lets create an image search service. First we create a form for entering the search content: echo "<form action='search.php' method='POST'>
<input type='search' name='search'>
<input type='submit' value='search images'>
</form>
";
Now we get a form which gets submitted to itself with the search text inside$_REQUEST['search']
Once we get this search string, we give it to the big guns to do the querying and
give us the results back in an understandable format. This is where Web Services come in.
So...now we are going to start the snippet that handles the POST event of the form
if($_SERVER['REQUEST_METHOD']=="POST")
{
Then we take the search string and url_encode it
This is necessary because we are going to pass it to yahoo as a GET method and spaces and
such characters can screwup if not url encoded.
$search = urlencode($_REQUEST['search']);
Now we are going to call the YAHOO WEB SERVICE URL and pass the encoded url string as
part of the query.
$request = 'http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo &query='.$search.'&results=50&output=php';
This is the WEB SERVICE url for YAHOO'S search service. Let us check the url parameters here.
appid = YahooDemo
query = $search
results= 50
output = php
appid = YahooDemo
Note that appid's value is YahooDemo. This means that the service URL here is only for demo and education purposes. For commercial usage of the service you might need to buy licences. More on this at the end of this article.
query = $search
As mentioned above this is the url encoded search string.
results = 50
This is the number of results to be fetched. If you increase this value the page might have problems loading. Pagination can be done based on this value. (Go, figure out how!)
output = php
This means that the request will return serialized PHP. If the output parameter is not mentioned YAHOO will output in XML. You can switch the output in JSON using. output = JSON
Ok..Now that we have constructed the service URL, lets query the results.
$response = file_get_contents($request);
This line will connect to the service URL and retrieve the first fifty results that YAHOO has returned as a serialised PHP Object. If you havent heard about Serialization before its a method used to ensure Object Persistance. If you havent heard about Object Persistance, you better start looking around some tutorials :) Wikipedia might be a good place to start.
Ok...so lets unserialize our response and store it inside $phpobj
$phpobj = unserialize($response);
LO AND BEHOLD! We got search results inside $phpobj. Just print_r it to see the results
echo '<pre>';
print_r($phpobj);
print_r($phpobj['ResultSet']['Result'][0]['Thumbnail']['Url']);
echo '</pre>';
} //close the POST method handler snippet
Well...if you are disappointed that no images were returned, then all you have to do is iterate the array and give the $phpobj url as the src for an <img> tag.
Happy Searching!
User login
Follow Us
Who's online
Who's new
- Nisha
- linnaeus
- Yameen
- TalleyReedy
- admin

