如何通过PHP获取Google Indexed页面数量

时间:2015-01-22 02:35:47

标签: php json google-api

您好我正在尝试创建一个脚本,该脚本应该检索来自Google的所有索引页面的计数。只有总页数(来自网站的结果:$ domain)

我找到了这个脚本:

function getGoogleCount($domain) {
$content = file_get_contents('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
$data = json_decode($content);
return intval($data->responseData->cursor->estimatedResultCount);
}

但是这个API很旧并且提供了不同的计数,那么获取索引页面的其他任何方式都是如此吗?

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php
function getpageindexed($name){
$weburl="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:".$name."&filter=0";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $weburl);
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$json = curl_exec($ch);
curl_close($ch);
$data=json_decode($json,true);
if($data['responseStatus']==200)
return $data['responseData']['cursor']['resultCount'];
else
return false;
}

$name="domainname.com"; //your domain name
echo getpageindexed($name); //get indexed page
?>