Taking a Look Inside
Let's take a look at the process we are going to use to retrieve search results from Google and get them onto our website. We will be doing this as a web service. WSDL, SOAP, and UDDI form the foundation standards of web services. We will be using SOAP and WSDL. The details of web services are beyond the scope of this article since we will only be working with a very basic example. We will also be using NuSOAP, thus, you should download the newest version at http://dietrich.ganx4.com/nusoap/.
An overview of what is going to happen is:
1 - Include the NuSOAP class
2 - Create a soapclient object
3 - Define an array of search criteria
4 - Make a SOAP request to Google
5 - Display the results returned
Lubing the Chains
Let's get started with the code and include NuSOAP.
<?php
include 'nusoap.php';
?>
?>
The second step is to create a soapclient object. This is defined in class.soapclient.php and it is required automatically by nusoap.php so you don't have to make any further inclusions. We are going to pass two parameters to the soapclient constructor. The first parameter is the URL to the webservice we are going to be using. The second parameter will specify that we are using WSDL. The code to create the soapclient object will look like this:
$soapclient
= new soapclient("http://api.google.com/GoogleSearch.wsdl","wsdl");
?>
The next step will be to build an array of parameters to specify the search criteria. We are going to define an associate array using the keys as the parameter name and the values as the parameter value. Looking at the GoogleSearch.wsdl file from the developer?s kit will tell us which parameters we can pass along with our SOAP request. We will be calling the doGoogleSearch method so let's locate that methods definition. The section we?re interested in looks like so:
<message name="doGoogleSearch">
<part name="key" type="xsd:string"/>
<part name="q" type="xsd:string"/>
<part name="start" type="xsd:int"/>
<part name="maxResults" type="xsd:int"/>
<part name="filter" type="xsd:boolean"/>
<part name="restrict" type="xsd:string"/>
<part name="safeSearch" type="xsd:boolean"/>
<part name="lr" type="xsd:string"/>
<part name="ie" type="xsd:string"/>
<part name="oe" type="xsd:string"/>
</message>
?>
For this example we are going to specify a value for each parameter listed above. Our criteria array would look something like this:
$params
= array('key' => 'YOUR_GOOGLE_KEY',
'q' => 'site:php.net updates',
'start' => 0,
'maxResults' => 10,
'filter' => false,
'restrict' => '',
'safeSearch' => false,
'lr' => '',
'ie' => '',
'oe' => ''
);
?>
This array specifies that the search results should include pages from the php.net website and contain the keyword "updates". We should receive search results starting from 0 and ending at 10. We have also omitted any filters, restrictions, and safesearch.
Now we are ready to make our search request. We will be using the "call" method of our soapclient object and passing two parameters. The first parameter we want to invoke is the webservice's method that we mentioned above. Remember, it is called doGoogleSearch. The second parameter we are going to pass is our search criteria array. The code to invoke the call method and save the response to a local array variable would look like this:
$result
= $soapclient->call("doGoogleSearch", $params);
?>
This will give us access to the search results via the multi-dimensional array $result. We can look at the GoogleSearch.wsdl file once again to see what type of data they have so kindly returned to us. The first thing we are going to do is to look and see how many results matched our search criteria. You may have noticed that there is a key in our array named estimatedTotalResultsCount. If we have more than 0 results we can loop through the results to display them. All of the data we are going to be accessing is defined in the GoogleSearch.wsdl file. Our display code will look like this:
if($result['estimatedTotalResultsCount'] > 0)
{
echo "Displaying {$result['estimatedTotalResultsCount']} result(s)<br /><br />";
foreach($result['resultElements'] as $v)
{
$title = $v['title'];
$url = $v['URL'];
$snippet = $v['snippet'];
echo "<a href={$url}>{$title}</a><br />{$snippet}<br /><br />";
}
}
else
{
echo 'Sorry but there are no search results that matched your criteria';
}
?>
That will display the number of search results followed by each result found. The title of the page will be linked to the URL. Below the link will be the snippet identical to the snippet you see on Google's search results page.
That is all you need to implement use of the Google Web API on your site using PHP and SOAP. An important note to be aware of is that each license key enables you to perform 1000 queries per day. This service is also in beta still and Google reserves the right to change the way it's implemented.
View Adding a Google Search to Your Site Discussion
Page: 1 2 Next Page: Keep It Simple (and Free)