上次PHP对接谷歌search console 第一篇
中已经讲述了基本的api *** 作。本篇主要是继续再记录一下search console 效果展示与网址检查部分的对接代码
官方效果图展示分为了三个部分
1.汇总数据
2.折线图数据
3.六个维度的数据
当维度参数不传值的时候就是汇总数据
上代码
declare(strict_types=1);
namespace App\Utils\Google;
class Query extends Base
{
/**
* 效果查询
* @param $token 授权最新的token
* @param $domain 资源ID
* @param $startDate 开始时间
* @param $endDate 结束时间
* @param $type 查询类型
* @param $dimension 查询维度
* @return \Google\Service\SearchConsole\SearchAnalyticsQueryResponse
*/
public function index($token,$domain,$startDate,$endDate,$type,$dimension='')
{
$webMaster = $this->getWebMaster($token);
$postBody = new \Google_Service_SearchConsole_SearchAnalyticsQueryRequest();
$postBody->setStartDate($startDate);
$postBody->setEndDate($endDate);
$postBody->setType($type);
if($dimension){
$postBody->setDimensions([$dimension]);
}
$response = $webMaster->searchanalytics->query($domain,$postBody);
return $response;
}
}
type支持参数值:
web 网络image 图片video 视频news 新闻dimension支持参数值:
DATE 日期QUERY 关键字PAGE 网页COUNTRY 国家/地区DEVICE 设备SEARCH_APPEARANCE 搜索外观 网址检查上代码
declare(strict_types=1);
namespace App\Utils\Google;
use App\Common\Log;
use Google\Service\SearchConsole\InspectUrlIndexRequest;
class Url extends Base
{
private $mark = 'GOOGLE_SEARCH_CONSOLE_INSPECT_URL';
/**
* 获取网址检查结果
* @param $token 刷新后最新的token
* @param $domain 资源ID
* @param $url 要查询的网址
* @return \Google\Service\SearchConsole\UrlInspectionResult
*/
public function inspect($token,$domain,$url)
{
$return = null;
try{
$request = new InspectUrlIndexRequest();
$domain = trim($domain,'/').'/';
$request->setSiteUrl($domain);
$request->setInspectionUrl($url);
$request->setLanguageCode('zh');
$webMaster = $this->getWebMaster($token);
$response = $webMaster->urlInspection_index->inspect($request);
Log::getInstance()->info('url inspect result',[$response]);
$return = $response->getInspectionResult();
}catch (\Throwable $e){
$this->errorHandle($e,$this->mark);
}
return $return;
}
//在这里做了一下返回值翻译的工作,可以根据具体需求调整
public function formatInspectResult(UrlInspectionResult $response)
{
$lastCrawlTimeStamp = $response->getIndexStatusResult()->getLastCrawlTime();
$mobileUsabilityResult = $response->getMobileUsabilityResult();
$return = [
'inspectionResultLink' => $response->getInspectionResultLink(),
'indexStatusResult' => [
'verdict' => $response->getIndexStatusResult()->getVerdict(),
'robotsTxtState' => $this->transRobotsTxtState($response->getIndexStatusResult()->getRobotsTxtState()),
'indexingState' => $this->transIndexingState($response->getIndexStatusResult()->getIndexingState()),
'lastCrawlTimeStamp' => is_null($lastCrawlTimeStamp)?null:strtotime($lastCrawlTimeStamp),
'pageFetchState' => $this->transPageFetchState($response->getIndexStatusResult()->getPageFetchState()),
'crawledAs' => $this->transCrawledAs($response->getIndexStatusResult()->getCrawledAs()),
]
];
if($mobileUsabilityResult){
$return['mobileUsabilityResult'] = $mobileUsabilityResult->getVerdict();
}
return $return;
}
private function transCrawledAs($value)
{
$arr = [
'CRAWLING_USER_AGENT_UNSPECIFIED' => '未知',
'DESKTOP' => 'Googlebot 桌面版',
'MOBILE' => 'Googlebot 智能手机版',
];
return $arr[$value]??'未知';
}
private function transPageFetchState($value)
{
$arr = [
'PAGE_FETCH_STATE_UNSPECIFIED' => '未知',
'SUCCESSFUL' => '成功',
'SOFT_404' => 'SOFT_404',
'BLOCKED_ROBOTS_TXT' => '被robots.txt阻止',
'NOT_FOUND' => '未找到(404)',
'ACCESS_DENIED' => '未经授权被阻止(401)',
'SERVER_ERROR' => '服务器错误(5**)',
'REDIRECT_ERROR' => '重定向错误',
'ACCESS_FORBIDDEN' => '访问被禁止 (403)',
'BLOCKED_4XX' => '其他(4**,非403、404)',
'INTERNAL_CRAWL_ERROR' => '内部错误',
'INVALID_URL' => '无效网址',
];
return $arr[$value]??'不适用';
}
private function transIndexingState($value)
{
$arr = [
'INDEXING_ALLOWED' => '是'
];
return $arr[$value]??'不适用';
}
private function transRobotsTxtState($value)
{
$arr = [
'ALLOWED' => '是'
];
return $arr[$value]??'不适用';
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)