使用PHP输出Google搜索结果?

时间:2017-01-02 12:28:16

标签: php search

我已经查看了stackoverflow上的几个旧答案,但它们都已过时,并且它们使用的API已不再可用。

我已经创建了一个JSON / Atom API,CX密钥并使用了一个脚本感谢Adam Fischer我在这里找到但是当我正在尝试我现在​​能够输出打印结果在页面上我得到了错误:

  

注意:未定义的属性:stdClass :: $ responseData in   第19行的E:\ XAMPP \ htdocs \ PHP Training \ google.php

     

注意:尝试在E:\ XAMPP \ htdocs \ PHP中获取非对象的属性   第19行训练\ google.php

这是我到目前为止所拥有的。下面的代码。

$url = 'https://www.googleapis.com/customsearch/v1?key=[MY API KEY]&cx=[MY CX KEY]&q=lecture';

$body = file_get_contents($url);
$json = json_decode($body);

for($x=0;$x<countif ($json->responseData->results);$x++>items){

echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echoforeach ($json->responseData->results[$x]->url;
echo>items "<br>VisibleURL:as ";$item){
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";print_r($item)
echo $json->responseData->results[$x]->content;
echo "<br><br>"; }
}

API工作正常,因为我访问时会吐出数组中的所有内容。示例:dl.dropboxusercontent.com/u/47731225/sample.txt

我正在尝试制作$ url我看到的结果会像Google搜索一样显示在我的页面上,例如:prntscr.com/drum5u

{
   "kind": "customsearch#result",
   "title": "The Tank, Haydon Allen Lecture Theatre, Building 23, ANU",
   "htmlTitle": "The Tank, Haydon Allen \u003cb\u003eLecture\u003c/b\u003e Theatre, Building 23, ANU",
   "link": "https://www.google.com/mymaps/viewer?mid=1YGFZHcZ20jPvy5OiaKT1voy841Q&hl=en",
   "displayLink": "www.google.com",
   "snippet": "\"The Tank\", Haydon Allen Lecture Theatre, Building 23, The Australian National \nUniversity (ANU), Canberra, Australia.",
   "htmlSnippet": "&quot;The Tank&quot;, Haydon Allen \u003cb\u003eLecture\u003c/b\u003e Theatre, Building 23, The Australian National \u003cbr\u003e\nUniversity (ANU), Canberra, Australia.",
   "cacheId": "hTeucZ5TewoJ",
   "formattedUrl": "https://www.google.com/mymaps/viewer?mid...hl=en",
   "htmlFormattedUrl": "https://www.google.com/mymaps/viewer?mid...hl=en",
   "pagemap": {
    "cse_thumbnail": [
     {
      "width": "221",
      "height": "228",
      "src": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSntx5YhQgJQeJ6RAZajOx7SGOwh0oUu8jtpY6VOAS75V_oNkiXx923ro4"
     }

2 个答案:

答案 0 :(得分:2)

您是否通过API查看了返回的json?我的猜测是,它与你期望的完全不同

  

https://developers.google.com/custom-search/json-api/v1/reference/cse/list

澄清后,您的结果与您的代码所期望的结果完全不同。

正确的代码应该是

$url = 'https://www.googleapis.com/customsearch/v1?key=[MY API KEY]&cx=[MY CX KEY]&q=lecture';

$body = file_get_contents($url);
$json = json_decode($body);
if ($json->items){
   foreach ($json->items as $item){
      print_r($item);
   }
}

答案 1 :(得分:1)

您可以使用文件获取内容来获取Google的完整页面内容,并且可以在您的网站中显示结果,例如

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$query = "search term";
$url = 'http://www.google.co.in/search?q='.urlencode($query).'';
$scrape = file_get_contents_curl($url);