Home > Article > Backend Development > How to access Google Search API with PHP, googleapi_PHP tutorial
The example in this article describes how PHP accesses Google Search API. Share it with everyone for your reference. The details are as follows:
This code snippet demonstrates how to send a request to the AJAX search API from php. Note that this example assumes PHP 5.2. For earlier installations of PHP, please refer to the corresponding official notes.
The specific code is as follows:
Copy code The code is as follows: $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.mysite.com/index.html");
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
I hope this article will be helpful to everyone’s PHP programming design.