• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

用PHP获取Google AJAX Search API 数据的代码_php技巧

php 搞代码 4年前 (2022-01-26) 12次浏览 已收录 0个评论

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>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:用PHP获取Google AJAX Search API 数据的代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址