http://code.google.com/apis/ajaxsearch/documentation/#fonje
Copy CodeThe code is as follows:
This example request includes a optional API key which you'll need to
Remove or replace with your own key.
Read more on why it's useful to has an API key.
The request also includes the Userip parameter which provides the end
User ' s IP address. Doing so would help distinguish this legitimate
Server-side traffic from traffic which doesn ' t come from an 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
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,/* Enter the URL of your site here */);
$body = curl_exec ($ch);
Curl_close ($ch);
Now, process the JSON string
$json = Json_decode ($body);
Now has some fun with the results ...
API KEY Application Address:
Http://code.google.com/apis/ajaxsearch/signup.html
Thus, we can write a function like this
Copy CodeThe code is 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 example
$rez = Google_search_api (Array (
' Q ' = ' 21andy.com ',//enquiry content
' Key ' = ' you apply to the API key ',
' Userip ' = ' Your IP address ',
));
Header (' content-type:text/html; charset=utf-8; ');
Echo ' <xmp>'; &lt;br&gt;print_r ($rez); &lt;br&gt;echo '</xmp> ';
http://www.bkjia.com/PHPjc/321322.html www.bkjia.com true http://www.bkjia.com/PHPjc/321322.html techarticle http://code.google.com/apis/ajaxsearch/documentation/#fonje Copy code code as follows://This example request includes an optional API key which you'll need to//remove or REPL ...