我使用谷歌搜索API来提取第80个第一个结果。我的问题是,每次运行程序时,它都会给我不同数量的结果(URL)。
注意:尝试在第42行获取非对象的属性
<?php
$query='site:https://fr.wikipedia.org%20sport';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&hl=fr&rsz=large&q=".$query;
$body = file_get_contents($url);
$json = json_decode($body);
$inF = fopen("fnm.txt","a");
for($x=0;$x<count($json->responseData->results);$x++){
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
fputs($inF,$json->responseData->results[$x]->url); fwrite($inF, "\r\n");
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
$j=0;
for ($i= 0; $i <= 10; $i++)
{
$j=$j+8;
echo $j;
$query = 'site:https://fr.wikipedia.org%20sport';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&hl=fr&rsz=large&start=$j&q=".$query;
$body = file_get_contents($url);
$json = json_decode($body);
for($x=0;$x<count($json->responseData->results);$x++){
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
fputs($inF,$json->responseData->results[$x]->url); fwrite($inF, "\r\n");
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
}
fclose ($inF);
答案 0 :(得分:0)
看看这个答案 - https://stackoverflow.com/a/4353393/5214904。 简而言之:64结果中没有API密钥存在限制
<强>被修改强>: 见https://developers.google.com/web-search/docs/#php-access 您需要在URL中发送您的/客户端IP,并在REFERER字段中发送您的站点。你可以用CURL来做。