Searching Google With The Google API
Handling The Returned Data From The Web Service
There is a quite a bit of information returned from performing a search with the API. We will only cover the essentials – if you’re really interested, you can simple var_dump() the results to see what else is available:
var_dump($result);
The returned data contains an array called resultElements, which contain the returned results. So if this array is empty, there were no results. To output them, we simply loop over this array and output each one.
Each result element contains the following data:
URL— the URL of the web sitetitle— the title of the web sitesnippet— a brief summary of the web site content
There are other fields it contains, that we don’t need right now.
Additionally, the returned data contains the following:
searchQuery— the search that was performedestimatedTotalResultsCount— the number of results (approximately) the search yieldedstartIndex— the position in the search of the first returned result (e.g. if we were fetching page 2)endIndex— the position in the search of the last returned resultsearchTime— the time in seconds it took Google to perform the search
Now, using all this data we can output the search results:
$numResults = count($result['resultElements']); <p> Search term: <strong> $result['searchQuery'] </strong> </p> if ($numResults) == 0) { <p> Your search yielded no results. </p> } else { <p> Results $result['startIndex'] to $result['endIndex'] of $result['estimatedTotalResultsCount'] </p> foreach ($result['resultElements'] as $row) { <h3><a href=" $row['URL'] "> $row['title'] </a></h3> <p> $row['snippet'] </p> <p> $row['URL'] </p> } }
And that’s all there is to it!
Now we can combine this with a simple search form to create our own Google frontend. This file will be our google.php file.

