How would you like your Google?
As an example of the kind of thing you can do with the code demonstrated here, I’ve prepared Google to RSS, a simple PHP script that searches Google for a given term and presents the results as an RSS feed. You could use this script to keep an eye on the top 10 search results for a topic you are interested in, or include them in a website. Feel free to experiment with the code.
If you’re not familiar with RSS, a simple understanding of XML or HTML should be enough to understand the basics of the this script. It follows the same structure as the snippet given previously: perform a SOAP query on the desired parameters, then loop through the resulting array and generate the output. Notice that it uses the UTF-8 character set, and your scripts should probably do so as well since this is the encoding that Google uses.
Advanced searching: restricts
doGoogleSearchwe had to supply a lot of extra arguments to control a wide range of features such as the number of results we want and whether SafeSearch should be enabled. Most of them are fairly easy to use and explained well in the Google Web API’s documentation, but restricts are a slightly more complex topic.
Restricts allow you to search from PHP a subset of Google’s database, so you could (for example) display sites from a certain country, in a certain language or about a particular topic. Here’s an example (replace the
doGoogleSearchcall in the code given above to try it out):
$queryResponse = $googleProxy->doGoogleSearch($key, 'PHP', 0, 10, true, 'countryUK', true, 'lang_it', '', '');
Access the page again, and while this is still a search for ‘PHP’, the results will be significantly different. Specifically, this only includes pages in Italian (that’s the ‘lang_it’ bit) that are hosted on servers in the United Kingdom (the ‘countryUK’ setting). This illustrates the difference between a language restrict and a country restrict. The former returns pages written in the desired language, wherever they are in the world; the latter returns pages from the specified country, regardless of the language they are written in.
There are lists of the available language and country restricts in the Google Web API’s documentation. Other than the two types demonstrated here, there are also topic restricts: try replacing ‘countryUK’ with ‘linux’ to get Italian pages about using PHP with Linux. Unfortunately, there aren’t many topics available yet – four at the time of writing – so this will probably be less useful.
Finally, we can combine multiple restricts using several operators. Prepend a minus sign (-) to remove all results that match the given restrict. Join two restricts with a period (.) to only include results that match both, or use a vertical bar (|) to include results that match either side. You can also use parentheses to group restricts for more complex expressions. Ever wanted to search for pages from Switzerland or Hungary that are related to Linux or the Apple Mac? No, me neither, but you can do it with the restrict ‘(countryCH|countryHU).(mac|linux)’.
We’ve studied the basics of how to consume a web service with PHP, examining in detail the main features provided by Google’s Web API. There’s lots more we could do with Google, and many more web services available across the Internet. You can find some through XMethods, or even create your own – check out the SOAP_Server class, for example. Good luck, and have fun!