duanjinchi1982 2014-10-01 01:07
浏览 39
已采纳

Google AnalyticsAPI包含路径

I am trying to set the include path dynamically in my code using what Google has supplied: set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/google-api-php-client/src');

I am still relatively noob to PHP and I am going to assume the /path/to/ needs to be changed to something else. I have tried setting it to /home/expiredf/public_html/google-api-php-client/srcBut that is not working.

Could someone please assist me to get through this HelloAnalyticsAPI.php tutorial?

Also before anyone links other questions with answers, I have looked through most of them and tried to implement their solutions but they are not working for me as they seem to be explained with a higher knowledge of PHP in mind. Basic and dumbed down answers are appreciated.

  • 写回答

1条回答 默认 最新

  • dongxi9326 2014-10-01 06:22
    关注

    The best answer to your question is to say don't use that tutorial. That tutorial is extremely out of date and uses the old PHP-client-Lib, this has been reported to Google.

    PHP client library

    The newest version of the Google PHP client library can be found on Github. google-api-php-client this client lib is updated regularly whenever anything changes. You will need to copy the entire src/Google directory to the directory of your application. I don’t recommend only taking the files that you need unless you really know what you are doing.

    Oauth2

    There are 3 steps to Oauth2 this is why it is called 3 legged authentication. In the first step you ask the user to give you access, second step the user gives you access, third and final step you exchange the access given to you by the user for the access to the data.

    <?php         
    require_once 'Google/Client.php';     
    require_once 'Google/Service/Analytics.php';       
    session_start();      
    $client = new Google_Client();
        $client->setApplicationName("Client_Library_Examples");
        $client->setDeveloperKey("{devkey}");  
        $client->setClientId('{clientid}.apps.googleusercontent.com');
        $client->setClientSecret('{clientsecret}');
        $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
        $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
    
        //For loging out.
        if ($_GET['logout'] == "1") {
        unset($_SESSION['token']);
           }   
    
        // Step 2: The user accepted your access now you need to exchange it.
        if (isset($_GET['code'])) {
    
            $client->authenticate($_GET['code']);  
            $_SESSION['token'] = $client->getAccessToken();
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        }
    
        // Step 1:  The user has not authenticated we give them a link to login    
        if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
            $authUrl = $client->createAuthUrl();
            print "<a class='login' href='$authUrl'>Connect Me!</a>";
            }        
    
        // Step 3: We have access we can now create our service
        if (isset($_SESSION['token'])) {
            print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
            $client->setAccessToken($_SESSION['token']);
            $service = new Google_Service_Analytics($client);    
    
            // request user accounts
            $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
    
           foreach ($accounts->getItems() as $item) {
            echo "Account: ",$item['name'], "  " , $item['id'], "<br /> 
    ";        
            foreach($item->getWebProperties() as $wp) {
                echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> 
    ";    
    
                $views = $wp->getProfiles();
                if (!is_null($views)) {
                    foreach($wp->getProfiles() as $view) {
                    //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> 
    ";    
                    }
                }
            }
        } // closes account summaries
    
        }
     print "<br><br><br>";
     print "Access from google: " . $_SESSION['token']; 
    ?>
    

    This code is ripped directly from my tutorial: Google Oauth2 PHP - Google Analytics API the tutorial is kept up to date. If this post is old you may want to see if there have been any changes in the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见