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

Source: Internet
Author: User
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>'; <br>print_r ($rez); <br>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 ...

  • 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.