Reputation: 27689
Could anyone provide an example or link on how to export TOP_QUERIES
from Google webmaster tools API?
There are no examples in the repository and there seems to be no available tutorial/example anywhere
https://github.com/google/google-api-php-client
This code connects to Webmaster Tools.. How to download the TOP_QUERIES
from a specific date interval?
require_once 'googleapi/autoload.php';
try{
$credentials = new Google_Auth_AssertionCredentials(
'262251945544-20r941rab6leiuo21ph7ielemtrcsmai@developer.gserviceaccount.com',
[Google_Service_Webmasters::WEBMASTERS_READONLY],
file_get_contents('9f21d103bb56.p12')
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if($client->getAuth()->isAccessTokenExpired()){
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Webmasters($client);
$t = $service->sites->get('https://example.com/');
print_r($t);
}
catch(Google_Exception $e){
echo $e->getMessage();
}
catch(Google_Service_Exception $e){
echo $e->getMessage();
}
Upvotes: 2
Views: 4498
Reputation: 11
After this line
$service = new Google_Service_Webmasters($client);
add this
$searchanalytics = $webmastersService->searchanalytics;
$request = new \Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$request->setStartDate('2016-03-01'); // startdate required
$request->setEndDate('2016-03-31'); // Enddate required
$dimensions[] = 'query';
$request->setDimensions($dimensions);
Than Replace this line
$results = $service->searchanalytics->query( $url, $search, $options )->getRows();
by
$results = $searchanalytics->query('site_URL', $request);
its work for Us ..... :)
Upvotes: 1
Reputation: 396
Assuming you have your Application setup correctly, here's an example of the approach I took:
// Authenticate through OAuth 2.0
$credentials = new Google_Auth_AssertionCredentials(
'[email protected]',
[Google_Service_Webmasters::WEBMASTERS_READONLY],
file_get_contents( 'path-to-your-key.p12' )
);
$client = new Google_Client();
$client->setAssertionCredentials( $credentials );
if ( $client->getAuth()->isAccessTokenExpired() ) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Webmasters($client);
// Setup our Search Analytics Query object
$search = new Google_Service_Webmasters_SearchAnalyticsQueryRequest;
$search->setStartDate( date( 'Y-m-d', strtotime( '1 month ago' ) ) );
$search->setEndDate( date( 'Y-m-d', strtotime( 'now' ) ) );
$search->setDimensions( array( 'query' ) );
$search->setRowLimit( 50 );
// Pass our Search Analytics Query object as the second param to our searchanalytics query() method
$results = $service->searchanalytics->query( $url, $search, $options )->getRows();
// Build a CSV
if ( ! empty( $results ) ) {
// Setup our header row
$csv = "Rank,Query,Clicks,Impressions,CTR,Position\r\n";
foreach ( $results as $key => $result ) {
// Columns
$columns = array(
$key + 1,
$result->keys[0],
$result->clicks,
$result->impressions,
round( $result->ctr * 100, 2 ) . '%',
round( $result->position, 1 ),
);
$csv .= '"' . implode( '","', $columns ) . '"' . "\r\n";
}
file_put_contents( dirname( __FILE__ ) . '/data.csv' );
}
I have a full article I just posted on my blog that has an example class I started to write as a wrapper for both Webmaster Tools API and Analytics API. Feel free to use this as a reference:
Upvotes: 0
Reputation: 32270
Error calling GET
https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fexample.com%2F: (404)
'https://example.com/'
is not a verified Webmaster Tools site in this account.
The site does not start with http://
, it starts with www.example.com
or example.com
. No trailing slashes either.
$t = $service->sites->get('example.com');
https://api.kdyby.org/class-Google_Service_Webmasters_SitesListResponse.html
Try those snippets:
<?php
$sites = $service->sites->listSites();
print_r(json_encode($sites->toSimpleObject());
or
<?php
$sites = $service->sites->listSites();
foreach ($sites as $site) {
print_r($site);
}
Upvotes: 0