2017-01-02 111 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列入黑名单,因为这严格违反了他们的服务条款。 –