2011-05-23 60 views
3

我從來沒有寫論壇,所以我希望我不會破壞你的代碼。但是我有一個關於Zend的OAuth功能的問題。如何在Google Analytics API中使用Zend OAuth?

我一直在嘗試使用它從Google Analytics API獲取提要,但我無法使其工作。

我的OAuth登錄工作正常,我可以使用它,例如Google Docs,沒問題。但Zend尚未支持Google Analytics。

我現在要發佈我的代碼,如果任何人有關於如何使用Zend的OAuth功能獲取Google Analytics的饋送的線索,我會很感激它 - 而且互聯網真的很缺乏這方面的信息話題!

而且,我寫它作爲一個WordPress插件,所以不顧一切的get_option和update_option等試想一下,你正在使用的會話,而不是:)

問候, 弗雷德裏克

編輯:哦,還有一件事。谷歌Analytics使用這種URL來獲得提要: 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(); 

回答

2

試試這個,

https://github.com/danielmitd/Zend_Gdata_Analytics/tree/master/Zend/Gdata

這不是官方的,但它的作品完美的我。原來的頁面看起來好像沒有,所以我不確定這些文檔有多難以追查。如果需要,我可以發佈一些示例。

+1

示例可以在這裏找到:[Zend_Gdata_Analytics](http://healthycod.in/2011/07/google-analytics-and-zend-framework/) – danielMitD 2012-02-26 12:45:46