用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.dhdzp.com/', $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)獲取當前時間(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自定義函數(shù)實現(xiàn)漢字轉(zhuǎn)換utf8編碼的方法
這篇文章主要介紹了php自定義函數(shù)實現(xiàn)漢字轉(zhuǎn)換utf8編碼的方法,涉及php針對字符串的遍歷、截取及編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2016-09-09
PHP實現(xiàn)cookie跨域session共享的方法分析
這篇文章主要介紹了PHP實現(xiàn)cookie跨域session共享的方法,結(jié)合實例形式分析了php操作cookie的有效期、跨域、session存儲等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
PHP執(zhí)行Curl時報錯提示CURL ERROR: Recv failure: Connection reset by
這篇文章主要介紹了PHP執(zhí)行Curl時報錯提示CURL ERROR: Recv failure: Connection reset by peer的解決方法,需要的朋友可以參考下2014-06-06
詳談配置phpstorm完美支持Codeigniter(CI)代碼自動完成(代碼提示)
下面小編就為大家?guī)硪黄斦勁渲胮hpstorm完美支持Codeigniter(CI)代碼自動完成(代碼提示)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
php獲得客戶端瀏覽器名稱及版本的方法(基于ECShop函數(shù))
這篇文章主要介紹了php獲得客戶端瀏覽器名稱及版本的方法,基于ECShop函數(shù)get_user_browser實現(xiàn)該功能,非常具有實用價值,需要的朋友可以參考下2015-12-12
PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作示例
這篇文章主要介紹了PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作,涉及mysql數(shù)據(jù)庫的創(chuàng)建以及php遞歸寫入、讀取數(shù)據(jù)庫分類相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

