This example assumes that we have used
a search box named yahooSearch:
// get the search terms
$query = rawurlencode(JRequest::getString('yahooSearch',
'Joomla!', 'DEFAULT', JREQUEST_ALLOWRAW));
We use the PHP rawurlencode() method because $query will be used in a URI. We
use the JREQUEST_ALLOWRAW mask so as not to lose any data from the request. There
is a full explanation of the JRequest masks in Chapter 11.
We make the assumption that if no search terms are provided we want to search for
Joomla!. In reality we would probably redirect the user.
??? ??? ??? ???
APIs and Web Services
[ 300 ]
Next we need to create the request URI from which we will obtain the
search results:
// Prepare the request URI
$request = 'http://search.yahooapis.com/WebSearchService/V1/
webSearch?appid=YahooDemo&query='.$query.'&results=4';
Now that we have the URI we can proceed to interact with the Yahoo! API. We
use the PHP function file_get_contents() to perform the request and retrieve
the results:
// Perform search
if (!$xml = file_get_contents($request))
{
// handle failed search request
}
The results of the request, if successful, are returned as an XML document.
Pages:
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413