Вопрос

I'm building a little backoffice for my site and I want to display the webmaster tools data within it, I cannot for the life of me figure this out!

Does anyone have any PHP examples of pulling data from webmaster tools using the API with PHP, I'm not getting the documentation and have found a php class that looks like it no longer works, but is there a working one already out there?

If I had an example to start with I think I could figure out the rest, I have been googling this for a couple of days now and have not had any success.

If I could just pull a list of sites belonging to my account that would be a start!!

For the record I have been digging through the docs at google, but I can't be the first person to have wanted to do this, so there must be people who have got this working!

Any Takers on throwing me a bone?

Iain

Это было полезно?

Решение

Here is a working example to fetch the sites list. I used my xhttp class, which is a PHP cURL wrapper, to hide the finer details in using cURL.

<?php

// Set account login info
$data['post'] = array(
  'accountType' => 'HOSTED_OR_GOOGLE',  // indicates a Google account
  'Email'       => '',  // full email address
  'Passwd'      => '',
  'service'     => 'sitemaps', // Name of the Google service
  'source'      => 'codecri.me-example-1.0' // Application's name'
);

// POST request
$response = xhttp::fetch('https://www.google.com/accounts/ClientLogin', $data);

// Extract Auth
preg_match('/Auth=(.+)/', $response['body'], $matches);
$auth = $matches[1];

$data = array();
$data['headers'] = array(
    'Authorization' => 'GoogleLogin auth="'.$auth.'"',
);

// GET request    
$response = xhttp::fetch('https://www.google.com/webmasters/tools/feeds/sites/', $data);

echo $response['body'];

?>

First thing the script does is to get an Authorization key via Google's ClientLogin. The service name used is sitemaps. You could also use OAuth or Oauth2 or AuthSub.

Next is to fetch the API URL endpoint for getting the sites list and just adding an Authorization header field.

UPDATE: APRIL 20, 2012 CLIENT LOGIN method as illustrated in the above script example won't work anymore because its deprecated by Google. See details here: https://developers.google.com/accounts/docs/AuthForInstalledApps

The best solution would be to use Oauth 2.0 to connect to Google webmaster tools API.

Другие советы

You can use this class to get data: This has been tested, help you to get TOP_PAGES, TOP_QUERIES,CRAWL_ERRORS, CONTENT_ERRORS,CONTENT_KEYWORDS, INTERNAL_LINKS, EXTERNAL_LINKS, SOCIAL_ACTIVITY

https://github.com/eyecatchup/php-webmaster-tools-downloads

Hope this may help you.

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(
    '1111111111-somebigemail@developer.gserviceaccount.com',
    [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:

http://robido.com/php/a-google-webmaster-tools-api-php-example-using-search-analytics-api-to-download-search-analytics-data-as-csv-with-the-new-oauth-2-0-method/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top