2017-01-02 143 views
0

我看了幾個舊的答案在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": "&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" 
    } 
+1

http://stackoverflow.com/questions/14055197/how-to-get-all-google-search-results-using-api –

+1

一旦你創建了一個問題,提供賞金,然後得到您承認的正確答案,不要將問題改爲新問題。如果正確,請接受有用的答案(或者標記爲什麼不正確),併爲您需要的新信息創建一個新問題。否則,歷史記錄對其他用戶沒有幫助(問題和答案不匹配),並且您沒有給予回答他們剛纔獎勵的用戶... – Robbie

+0

感謝您讓我知道Robbie。 我已經改變了一切,回到它應該如何。 – squidg

回答

2

你去翻從API返回的JSON顯示結果?我的猜測是,這是完全不同的,對你的期望是什麼

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

clarifiacation後,結果與預期你的代碼真的不同。

正確的代碼看起來應該像

$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); 
    } 
} 
+0

當我直接訪問網址時,我可以從API獲取返回的JSON,但我不太清楚如何獲取該數據並將其顯示在我的頁面上 – squidg

+0

您可以將返回的JSON粘貼到某處供我查看嗎? –

+0

https://dl.dropboxusercontent.com/u/47731225/JSON.txt – squidg

1

您可以使用該文件獲取內容,以獲得谷歌的整頁內容,您可以在您的網站,如

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); 
+1

喜歡你的回答 – mghhgm

+0

如果你使用上述方法來做到這一點,請準備好將Google列入黑名單,因爲這嚴格違反了他們的服務條款。 –