Search code examples
phpgoogle-api-php-clientgoogle-search-consolegoogle-api-webmasters

Webmaster Tools: Google PHP API client only single result?


I'm trying to pull a list of search queries from Google's Search Console API using the latest beta PHP API client. Auth works fine, permissions are OK.

However, when I try to pull a list of search queries I only receive a single result despite using a dimension (query):

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

// Build query
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest;
$request->setStartDate('2015-06-01');
$request->setEndDate('2015-06-02');
$request->setDimensions("[query]");

$qsearch = $searchanalytics->query("http://www.mydomain.xyz", $request); 
$rows = $qsearch->getRows();

print_r($rows) returns something similar to this, a summary of all clicks during the date range instead of a list of search keywords:

Array ( [0] => Google_Service_Webmasters_ApiDataRow Object (
[collection_key:protected] => keys [internal_gapi_mappings:protected] =>
Array ( ) [clicks] => 210 [ctr] => 0.1028823282 [impressions] => 2095
[keys] => [position] => 4.093283557047 [modelData:protected] =>
Array ( ) [processed:protected] => Array ( ) ) )

I've ploughed my way through the dev manual, the search console works fine and shows all results, but my API query doesn't seem to work properly.

Appreciate any help and ideas. Thanks!


Solution

  • If I'm not mistaken, dimensions should be passed as an array of strings instead of just a string. Try the following:

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