Code _php tips for getting Google AJAX Search API data in PHP

Source: Internet
Author: User

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> ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.