http://code.google.com/apis/ajaxsearch/documentation/
Copy Code code as follows:
This example request includes a optional API key which you would need to
Remove or replace with your own key.
Read more about why it's useful to have API key.
The request also includes the Userip parameter which provides the end
User ' s IP address. Doing so'll help distinguish this legitimate
Server-side traffic from traffic which doesn ' t come from a end-user.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "Q=paris%20hilton&key=insert-your-key&userip=users-ip-address";
SendRequest
How to Referer is set manually
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_referer,/* Enter the URL of your site here * *);
$body = curl_exec ($ch);
Curl_close ($ch);
Now, process the JSON string
$json = Json_decode ($body);
Now have some fun with the results ...
API KEY Request Address:
Http://code.google.com/apis/ajaxsearch/signup.html
So, we can write a function like this
Copy Code code as follows:
function Google_search_api ($args, $referer = ' http://www.jb51.net/', $endpoint = ' web ') {
$url = "http://ajax.googleapis.com/ajax/services/search/". $endpoint;
if (!array_key_exists (' V ', $args))
$args [' V '] = ' 1.0 ';
$url. = '? '. Http_build_query ($args, ', ' & ');
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_referer, $referer);
$body = curl_exec ($ch);
Curl_close ($ch);
Return Json_decode ($body);
}
Using the sample
$rez = Google_search_api (Array (
' Q ' => ' 21andy.com ',//inquiry content
' Key ' => ' the API key ' you're applying to ',
' Userip ' => ' Your IP address ',
));
Header (' content-type:text/html; charset=utf-8; ');
Echo ' <xmp> ';
Print_r ($rez);
Echo ' </xmp> ';