As I stated in the prior section, the results returned by a given Yahoo! Web Service can be retrieved in the form of a PHP serialized array by specifying an “output=php” option within the corresponding query string. To see in detail how this process works, study the following code sample. It first queries the Yahoo! Web Search service with the same “Madonna” search term, and then returns the results as a serialized PHP array. Here it is: try{ // example using Yahoo! Web Search Service - results are displayed specifying PHP output $request='http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid= // trigger the http request if(!$results=file_get_contents($request)){ throw new Exception(‘Error requesting Yahoo! Web service’); } $results=unserialize($results); echo ‘print_r($results,TRUE)’; } catch(Exception $e){ echo $e->getMessage(); exit(); } In the previous example, you can see that I specified that all of the search results returned by the Yahoo! Web Search Service must be retrieved in the form of a serialized PHP array. In this case, the output generated by the previous script would be similar to this: Array ( [ResultSet] => Array ( [type] => web [totalResultsAvailable] => 65700000 As you can see, all the results produced by the corresponding search term have been returned in a serialized array, which was unserialized before being outputted to the browser. Although this array is actually multidimensional, it’s quite easy to extract certain elements for further processing. Quite simple to grasp, right? At this point you learned not only how to implement the Yahoo! Web Search Service with PHP 5, but how to return the results as a serialized PHP array. Nevertheless, you're probably wondering how this feature can be exploited in a useful manner. Well, as you saw in the previous example, it’s fairly simple to extract some of these array elements to display them in a more readable format using some fancy markup. Thus, in the next section I’m going to teach you how perform this task with minor efforts, so click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|