用PHP获取Google AJAX Search API 数据的代码
2010-03-12 00:00
951 查看
http://code.google.com/apis/ajaxsearch/documentation/#fonje
API KEY 申请地址:
http://code.google.com/apis/ajaxsearch/signup.html
由此,我们可以写个函数像这样
// This example request includes an optional API key which you will need to // remove or replace with your own key. // Read more about why it's useful to have an API key. // The request also includes the userip parameter which provides the end // user's IP address. Doing so will 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 have some fun with the results...
API KEY 申请地址:
http://code.google.com/apis/ajaxsearch/signup.html
由此,我们可以写个函数像这样
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); } // 使用示例 $rez = google_search_api(array( 'q' => '21andy.com', // 查询内容 'key' => '你申请到的API KEY', 'userip' => '你的IP地址', )); header('Content-type: text/html; charset=utf-8;'); echo '<xmp>'; print_r($rez); echo '</xmp>';
相关文章推荐
- http://www.ajaxlines.com/ajax/stuff/article/using_google_is_ajax_search_api_with_java.php
- 掌握 Ajax,第 9 部分: 使用 Google Ajax Search API
- jQuery中使用Ajax获取JSON格式数据示例代码
- php获取新浪微博数据API实例
- Ajax 通过城市名获取数据(全国天气预报API)
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)
- 利用google提供的API(JavaScript接口)获取网站访问者IP地理位置的代码详解
- PHP访问Google Search API的方法
- PHP下使用CURL方式POST数据至API接口的代码
- XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)
- 新的Google Ajax Search Api
- 掌握 Ajax,第 9 部分: 使用 Google Ajax Search API
- ajax从数据库获取数据的代码
- 掌握 Ajax,第 9 部分: 使用 Google Ajax Search API
- php 获取百度的热词数据的代码
- PHP在线生成二维码(google api)的实现代码详解
- PHP在线生成二维码(google api)的代码
- PHP在线生成二维码(google api)的实现代码详解