我从不在论坛上写作,所以我希望我不会破坏您的手抄本。但我对Zend的OAuth功能有疑问。
我一直在尝试使用它从Google Analytics API获取Feed,但我无法让它工作。
我已经让OAuth登录工作得很好,我可以将它与Google Docs一起使用,没问题。但Zend尚未支持Google Analytics。
我现在要发布我的代码,如果有人知道如何使用Zend的OAuth功能获取Google Analytics的数据,我会很感激 - 并且互联网真的缺乏关于此主题的信息!
另外,我把它写成Wordpress插件,所以忽略所有的get_option和update_option等。想象一下你正在使用Sessions:)
此致 弗雷德里克
编辑:哦,还有一件事。 Google Analytics使用此类网址获取Feed: https://www.google.com/analytics/feeds/data?ids=ga%3A0000000&metrics=ga%3Apageviews&start-date=2011-05-09&end-date=2011-05-23&max-results=50 $consumerKey = 'XXX';
$secret = 'XXX';
require_once 'Zend/Loader.php';
Zend_Loader::loadClass( 'Zend_Gdata_HttpClient' );
Zend_Loader::loadClass( 'Zend_Gdata_Docs' );
Zend_Loader::loadClass( 'Zend_Gdata_Spreadsheets' );
Zend_Loader::loadClass( 'Zend_Oauth_Consumer' );
Zend_Loader::loadClass( 'Zend_Http_Client' );
Zend_Loader::loadClass( 'Zend_Gdata_Gbase' );
// set your Google consumer key / secret
$CONSUMER_KEY = $consumerKey;
$CONSUMER_SECRET = $secret;
$RETURN_TO = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
// Multi-scoped token.
// Sorry! I'm using the analytics scope at this particular place in the code. In the bottom of this code though, there's some request code that needs the Google Docs scope. If you want a working example for Google Docs, change this scope accordingly.
$SCOPES = array(
'https://www.google.com/analytics/feeds/',
);
$oauthOptions = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET,
'signatureMethod' => 'HMAC-SHA1',
'callbackUrl' => $RETURN_TO,
'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
);
if ( trim( $accessToken ) == '' ) {
$consumer = new Zend_Oauth_Consumer( $oauthOptions );
echo 'yes 1';
update_option( 'ni_trends_google_request_token', serialize( $consumer->getRequestToken( array( 'scope' => implode( ' ', $SCOPES ) ) ) ) );
$approvalUrl = $consumer->getRedirectUrl( array( 'hd' => 'default' ) );
echo '<a href="' . $approvalUrl . '">Grant access</a>';
if ( trim( $accessToken ) == '' ) {
update_option( 'ni_trends_google_access_token', serialize( $consumer->getAccessToken( $_GET, unserialize( $requestToken ) ) ) );
}
update_option( 'ni_trends_google_request_token', '' );
}
$accessToken = unserialize( $accessToken );
// This is where I run into trouble. This is for Google Docs, and it's working (although I have the Analytics Scope configured at the moment) - but how do I make my request and fetch the feed from Google Analytics?
$httpClient = $accessToken->getHttpClient($oauthOptions);
$client = new Zend_Gdata_Docs($httpClient, "yourCompany-YourAppName-v1");
$feed = $client->getDocumentListFeed();
echo "<pre>";
echo "<ul>\n";
foreach ($feed->entries as $entry) {
echo "<li>$entry->title </li>\n";
}
echo "</ul>\n";
echo "</pre>\n";
编辑:解决了以下问题!
$accessToken = unserialize( $accessToken );
$client = $accessToken->getHttpClient( $oauthOptions );
$client->resetParameters();
$parameters = array(
'ids' => 'ga:26870853',
'metrics' => 'ga:pageviews',
'start-date' => '2010-01-08',
'end-date' => '2011-05-22',
'max-results' => '50'
);
$client->setUri('https://www.google.com/analytics/feeds/data');
$client->setParameterGet($parameters);
$client->setMethod(Zend_Http_Client::GET);
$response = $client->request();
print_r( $response );
exit();
答案 0 :(得分:2)
试试这个,
https://github.com/danielmitd/Zend_Gdata_Analytics/tree/master/Zend/Gdata
这不是官方的,但它对我来说很完美。原来的页面看起来很糟糕,所以我不确定文档跟踪的难度。如果需要,我可以发布一些例子。