网站管理员工具API和PHP
我正在为我的网站构建一个小后台,并且我想在其中显示网站管理员工具数据,但我不能为我的生活弄清楚这一点!网站管理员工具API和PHP
有没有人有任何使用PHP使用API从网站管理员工具拉取数据的PHP示例,我没有收到文档并找到一个看起来不再有效的php类,但是已经有工作了在那里?
如果我有一个例子开始,我想我可以找出其余的,我已经使用这个搜索了几天,并没有取得任何成功。
如果我只能拉一个属于我的帐户,这将是一个开始的网站列表!
为了记录我一直在挖掘文档在谷歌,但我不能成为第一个想要做到这一点的人,所以一定有人得到这个工作!
任何摔跤手都会把我扔骨头吗?
Iain
这里是一个工作示例来获取站点列表。我使用我的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 or Oauth2 or AuthSub。
接下来是获取API URL端点以获取站点列表并仅添加一个Authorization
标题字段。
更新日期:2012年4月20日 上述脚本示例中所示的CLIENT LOGIN方法将不再适用,因为它已被Google弃用。在此处查看详细信息:https://developers.google.com/accounts/docs/AuthForInstalledApps
最好的解决方案是使用Oauth 2.0连接到Google网站管理员工具API。
你可以使用这个类来获得数据:这已经过测试,帮助您获得 TOP_PAGES,TOP_QUERIES,CRAWL_ERRORS,CONTENT_ERRORS,CONTENT_KEYWORDS,INTERNAL_LINKS,EXTERNAL_LINKS,SOCIAL_ACTIVITY
https://github.com/eyecatchup/php-webmaster-tools-downloads
但愿这帮你。
假设你正确有你的应用程序的设置,这里是我采取的方法的一个例子:我开始写的
// 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');
}
我有一个完整的文章中,我只是贴在我的博客上有一个例子类适用于网站站长工具API和Analytics API的封装。随意使用此作为参考:
我正在寻找这一点。到目前为止,我发现的只有这个:** 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/zh-CN/apis/webmastertools/ docs/2.0/reference.html只要我得到更多,我会更新此评论。 – Roger 2011-06-10 13:25:58