假設你正確有你的應用程序的設置,這裏是我採取的方法的一個例子:我開始寫的
// 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的封裝。隨意使用此作爲參考:
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/
我正在尋找這一點。到目前爲止,我發現的只有這個:** 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