我看了幾個舊的答案在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