http://code.google.com/apis/ajaxsearch/documentation/#fonje
<BR>// This example request includes an optional API key which you will need to <BR>// remove or replace with your own key. <BR>// Read more about why it's useful to have an API key. <BR>// The request also includes the userip paramet<span style="color:transparent">/本文来源gaodai#ma#com搞*!代#%^码网%</span><sub>搞代gaodaima码</sub>er which provides the end <BR>// user's IP address. Doing so will help distinguish this legitimate <BR>// server-side traffic from traffic which doesn't come from an end-user. <BR>$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" <BR>. "q=Paris%20Hilton&key=INSERT-YOUR-KEY&userip=USERS-IP-ADDRESS"; <br><br>// sendRequest <BR>// note how referer is set manually <BR>$ch = curl_init(); <BR>curl_setopt($ch, CURLOPT_URL, $url); <BR>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <BR>curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */); <BR>$body = curl_exec($ch); <BR>curl_close($ch); <br><br>// now, process the JSON string <BR>$json = json_decode($body); <BR>// now have some fun with the results... <BR>
API KEY 申请地址:
http://code.google.com/apis/ajaxsearch/signup.html
由此,我们可以写个函数像这样
<BR>function google_search_api($args, $referer = 'http://www.gaodaima.com/', $endpoint = 'web'){ <BR>$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint; <BR>if ( !array_key_exists('v', $args) ) <BR>$args['v'] = '1.0'; <BR>$url .= '?'.http_build_query($args, '', '&'); <BR>$ch = curl_init(); <BR>curl_setopt($ch, CURLOPT_URL, $url); <BR>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <BR>curl_setopt($ch, CURLOPT_REFERER, $referer); <BR>$body = curl_exec($ch); <BR>curl_close($ch); <BR>return json_decode($body); <BR>} <br><br>// 使用示例 <BR>$rez = google_search_api(array( <BR>'q' => '21andy.com', // 查询内容 <BR>'key' => '你申请到的API KEY', <BR>'userip' => '你的IP地址', <BR>)); <BR>header('Content-type: text/html; charset=utf-8;'); <BR>echo ''; <BR>print_r($rez); <BR>echo ''; <BR>