Dimensions of query webmasters tools api

南笙酒味 提交于 2019-12-23 05:38:11

问题


specially Alex :)

I want to know if any body have a PHP code to get the details of a query from webmasters tools api.

I have already the query dimensions but I dont't know how exactely to make it with PHP code.

$webmastersService = new Google_Service_Webmasters($client);
$searchanalytics = $webmastersService->searchanalytics;
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest;

回答1:


Supposing, that you have all credentials and tokens. If you don't have them, you'll get (401) Login Required error.

Making request you can set startDate, endDate, searchType, rowLimit via setter methods like this:

$query->setStartDate('2015-11-10');

But some methods require array like setDimensions:

$query->setDimensions(array('page'));

To more complicate the things setDimensionFilterGroups method requires array of Google_Service_Webmasters_ApiDimensionFilterGroup . And every Google_Service_Webmasters_ApiDimensionFilterGroup instance requires filters to be set via setFilters method with an array of Google_Service_Webmasters_ApiDimensionFilter.

And for Google_Service_Webmasters_ApiDimensionFilter you can set dimension, operator and expression via setDimension, setOperator, setExpression methods.

For additional info on these types, classes and methods please refer to https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Webmasters.php

Consider, you want pages (dimensions=page) the given day (startdate, enddate) and filter results for a given search query. To create a filter you need to set dimension to query, operator to equals and expression to your keyword.

This request in API Explorer looks like:

So the code to get all pages of example.com site that were displayed 2015-11-10 in reply to "weird things" search query is below:

    $query = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
    $query->setDimensions(array('page'));
    $query->setStartDate('2015-11-10');
    $query->setEndDate('2015-11-10');

    $filter = new Google_Service_Webmasters_ApiDimensionFilter();
    $filter->setDimension('query');
    $filter->setOperator('equals');
    $filter->setExpression('weird things');

    $filtergroup = new Google_Service_Webmasters_ApiDimensionFilterGroup();
    $filtergroup->setFilters(array($filter));
    $query->setDimensionFilterGroups(array($filtergroup));

    $response = $service->searchanalytics->query('http://example.com/', $query);

That is simplified demo code. May be it has some mistakes.

And I want to note, that Python API is much easier and clearer.



来源:https://stackoverflow.com/questions/33939113/dimensions-of-query-webmasters-tools-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!