我看了几个旧的答案在stackoverflow,但他们都过时了,他们使用的API不再可用。使用PHP输出Google搜索结果?
我创建了一个JSON/Atom API,CX键并使用了一个脚本感谢Adam Fischer我在这里找到了,但是当我试着现在能够输出打印页面上的结果时,错误:
Notice: Undefined property: stdClass::$responseData in E:\XAMPP\htdocs\PHP Training\google.php on line 19
Notice: Trying to get property of non-object in E:\XAMPP\htdocs\PHP Training\google.php on line 19
这是我到目前为止。下面的代码。
$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": ""The Tank", 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"
}
http://stackoverflow.com/questions/14055197/how-to-get-all-google-search-results-using-api –
一旦你创建了一个问题,提供赏金,然后得到您承认的正确答案,不要将问题改为新问题。如果正确,请接受有用的答案(或者标记为什么不正确),并为您需要的新信息创建一个新问题。否则,历史记录对其他用户没有帮助(问题和答案不匹配),并且您没有给予回答他们刚才奖励的用户... – Robbie
感谢您让我知道Robbie。 我已经改变了一切,回到它应该如何。 – squidg