用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼
更新時間:2010年03月12日 00:03:58 作者:
用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼
http://code.google.com/apis/ajaxsearch/documentation/#fonje
復(fù)制代碼 代碼如下:
// 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
由此,我們可以寫個函數(shù)像這樣
復(fù)制代碼 代碼如下:
function google_search_api($args, $referer = 'http://www.dbjr.com.cn/', $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', // 查詢內(nèi)容
'key' => '你申請到的API KEY',
'userip' => '你的IP地址',
));
header('Content-type: text/html; charset=utf-8;');
echo '<xmp>';
print_r($rez);
echo '</xmp>';
您可能感興趣的文章:
- PHP 利用AJAX獲取網(wǎng)頁并輸出的實現(xiàn)代碼(Zjmainstay)
- AJAX 動態(tài)獲取當(dāng)前時間(php)
- ajax獲取php頁面的返回參數(shù),控件賦值的方法
- PHP Ajax JavaScript Json獲取天氣信息實現(xiàn)代碼
- php獲取ajax的headers方法與內(nèi)容實例
- php+ajax實現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
- php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實例
- ajax處理php返回json數(shù)據(jù)的實例代碼
- Ajax+php數(shù)據(jù)交互并且局部刷新頁面的實現(xiàn)詳解
- PHP+ajax實現(xiàn)獲取新聞數(shù)據(jù)簡單示例
相關(guān)文章
php鏈?zhǔn)讲僮鱩ysql數(shù)據(jù)庫(封裝類帶使用示例)
本文代碼將一些簡單常用的SQL語句,拆分、封裝成鏈?zhǔn)胶瘮?shù)與終結(jié)函數(shù),鏈?zhǔn)讲僮鳑]有先后之分,實現(xiàn)傻瓜式mysql數(shù)據(jù)庫操作。?同時學(xué)習(xí)下靜態(tài)成員函數(shù),實現(xiàn)鏈?zhǔn)讲僮鞯木唧w語法。2023-02-02PHP實現(xiàn)將Word文件保存到SQL Server數(shù)據(jù)庫
這篇文章主要介紹了如何利用PHP實現(xiàn)將上傳的Word文件保存到SQL Server數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02PHP計劃任務(wù)之關(guān)閉瀏覽器后仍然繼續(xù)執(zhí)行的函數(shù)
通過使用ignore_user_abort函數(shù)的特性,實現(xiàn)PHP計劃任務(wù),需要的朋友可以參考下。2010-07-07PHP類的靜態(tài)(static)方法和靜態(tài)(static)變量使用介紹
PHP類的靜態(tài)(static)方法和靜態(tài)(static)變量使用介紹,學(xué)習(xí)php的朋友可以看下2012-02-02