使用PHP输出Google搜索结果?

问题描述 投票:0回答:2

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

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

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

注意:尝试在第19行的E:\ XAMPP \ htdocs \ PHP Training \ 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"
     }
php search
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.