本文实例讲述了PHP访问Google Search API的方法。分享给大家供大家参考。具体如下:
这段代码段演示了如何从php向AJAX搜索API发送请求。请注意,此示例假定使用 PHP 5.2。对于较早安装的 PHP,请参考对应的官方注释。
具体代码如下:
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong><strong>搞gaodaima代码</strong>Paris%20Hilton";<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, "http://www.mysite.com/index.html");<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...
希望本文所述对大家的php程序设计有所帮助。