Webmaster工具API和PHP

4
我正在为我的网站建立一个小型后台,我想在其中显示Webmaster工具数据,但我无法弄清楚如何实现!
是否有任何PHP示例可以使用API从Webmaster工具中获取数据?我无法理解文档,并发现一个看起来不再起作用的php类,但是否已经有一个可用的工作类?
如果我有一个示例开始,我认为我可以解决其余部分。我已经搜索了几天,但没有任何成功。
如果我只能拉出属于我的帐户的站点列表,那就是一个开始!!
记录一下,我一直在查阅谷歌的文档,但我肯定不是第一个想要这样做的人,所以一定有人已经将其工作起来了!
有没有人愿意帮我一把?
艾恩

我也在寻找这个。到目前为止,我找到的只有:Zend Gdata http://code.google.com/apis/gdata/articles/php_client_lib.html http://code.google.com/apis/gdata/articles/php_client_lib.html#gdata-installation http://framework.zend.com/download/gdata/ 其他PHP http://www.phpclasses.org/browse/file/30954.html 和 http://www.simplesoft.it/google-webmaster-tools-api-in-php.html API参考 http://code.google.com/intl/en/apis/webmastertools/docs/2.0/reference.html 我一旦找到更多信息,就会更新这个评论。 - Roger
3个回答

3

这里有一个能够获取网站列表的工作示例。我使用了我的xhttp class,它是一个PHP cURL包装器,用于隐藏使用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'];

?>

脚本的第一步是通过Google的ClientLogin获取授权密钥。使用的服务名称是sitemaps。您也可以使用OAuth或Oauth2或AuthSub

接下来是获取站点列表的API URL端点,只需添加一个Authorization标头字段。

更新:2012年4月20日,上述脚本示例中的CLIENT LOGIN方法将不再适用,因为它已被Google弃用。请参阅此处的详细信息:https://developers.google.com/accounts/docs/AuthForInstalledApps

最好的解决方案是使用Oauth 2.0连接到Google网站管理员工具API。


0
假设您已经正确设置了应用程序,这是我采取的方法的示例:
// 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' );
}

我在我的博客上发布了一篇完整的文章,其中包含一个示例类,我开始编写它作为Webmaster工具API和Analytics API的包装器。请随意将其用作参考:

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/


0
您可以使用这个类来获取数据:已经测试过,可以帮助您获取TOP_PAGES、TOP_QUERIES、CRAWL_ERRORS、CONTENT_ERRORS、CONTENT_KEYWORDS、INTERNAL_LINKS、EXTERNAL_LINKS和SOCIAL_ACTIVITY。

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

希望这可以对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接