Php 如何在不登录的情况下使用谷歌api获取分析数据?

Php 如何在不登录的情况下使用谷歌api获取分析数据?,php,google-analytics,google-analytics-api,Php,Google Analytics,Google Analytics Api,我正在使用php和mysql。我必须使用GoogleSDK api获取不同站点的分析数据,最重要的是我不需要登录api。没有登录,我需要去获取分析数据 请看下面的代码,我可以在其中获取“Google_AnalyticsService”对象,并且在获取数据时遇到错误。它表示需要登录… require 'apigp/Google_Client.php'; require 'apigp/contrib/Google_PlusService.php'; require 'apigp/contrib/Go

我正在使用php和mysql。我必须使用GoogleSDK api获取不同站点的分析数据,最重要的是我不需要登录api。没有登录,我需要去获取分析数据
请看下面的代码,我可以在其中获取“Google_AnalyticsService”对象,并且在获取数据时遇到错误。它表示需要登录…

require 'apigp/Google_Client.php';
require 'apigp/contrib/Google_PlusService.php';
require 'apigp/contrib/Google_AnalyticsService.php';

date_default_timezone_set('UTC');
$date_beforeonemoth = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-30,date("Y")));
$date_today = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")));

$google = new Google_Client();
$google->setDeveloperKey('Google AppKey here');
$google->setClientSecret('Google ClientSecretKey here');
$google->setClientId('Google ClientID here');
$analytics = new Google_AnalyticsService($google);

$profileURL="";
$pageName="";
$brandId="";
$sql="select * from profiles where APIType='Google Plus'";
$result=mysql_query($sql);
while($rs = mysql_fetch_assoc($result))
{
    $profileURL='https://plus.google.com/105932700878744xxxxxx';
    $brandId=4;
    $pageName=basename($profileURL);

    $accounts = $analytics->management_accounts->listManagementAccounts();
    if(isset($accounts['totalResults'])){
        if($accounts['totalResults']>0){
            $firstAccountId = $accounts['items'][0]['id'];
 $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);
                if(count($webproperties['items'])>0){
                     $firstWebpropertyId = $webproperties['items'][0]['id'];
$profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

                     if (count($profiles['items']) > 0) {
                       $profileId = $profiles['items'][0]['id'];


        $profileId = $pageName;
        $results = $analytics->data_ga->get(
        'ga:' . $profileId,
        $date_beforeonemoth,
        $date_today,
       'ga:visits,ga:pageviews,ga:pageviewsPerVisit,ga:avgTimeOnPage,ga:visitBounceRate,ga:percentNewVisits,ga:uniquePageviews',
        array(
            'dimensions' => 'ga:source,ga:keyword,ga:browser,ga:date,ga:month,ga:day,ga:year',
            'sort' => '-ga:visits,ga:keyword',
           // 'filters' => 'ga:medium==organic',
            'max-results' => '25'));

                       $visits = "";
                       $unique_visitors = "";
                       $pageviews = "";
                       $pages_visit = "";
                       $avg_visit_duration = "";
                       $bounce_rate = "";
                       $new_visits = "";

                       echo "<br>".$visits = $results['totalsForAllResults']['ga:visits'];
                       echo "<br>".$unique_visitors = $results['totalsForAllResults']['ga:uniquePageviews'];
                       echo "<br>".$pageviews = $results['totalsForAllResults']['ga:pageviews'];
                       $pages_visit = $results['totalsForAllResults']['ga:pageviewsPerVisit'];
                       $avg_visit_duration = $results['totalsForAllResults']['ga:avgTimeOnPage'];
                       $bounce_rate = $results['totalsForAllResults']['ga:visitBounceRate'];
                       $new_visits = $results['totalsForAllResults']['ga:percentNewVisits'];

                       $profileName = $results['profileInfo']['profileName'];

                    }else{print"No profiles found for this user.";}

                    }else{print"No webproperties found for this user.";}  //close webproperties result if condition

                }else{print"No accounts found for this user.";} //close total result if condition   
}
}
需要'apigp/Google_Client.php';
需要'apigp/contrib/Google_PlusService.php';
需要'apigp/contrib/Google_AnalyticsService.php';
日期\默认\时区\设置('UTC');
$date_beforeonemoth=日期(“Y-m-d”),mktime(0,0,0,日期(“m”),日期(“d”)-30,日期(“Y”);
$date_today=日期(“Y-m-d”,mktime(0,0,0,日期(“m”),日期(“d”),日期(“Y”);
$google=新的google_客户端();
$google->setDeveloperKey('googleappkey here');
$google->setClientSecret('google ClientSecretKey here');
$google->setClientId('googleclientid here');
$analytics=新的谷歌分析服务($Google);
$profileURL=“”;
$pageName=“”;
$brandId=“”;
$sql=“从配置文件中选择*,其中APIType='Google Plus';
$result=mysql\u查询($sql);
而($rs=mysql\u fetch\u assoc($result))
{
$profileURL='0https://plus.google.com/105932700878744xxxxxx';
$brandId=4;
$pageName=basename($profileURL);
$accounts=$analytics->management_accounts->listManagementAccounts();
如果(isset($accounts['totalResults'])){
如果($accounts['totalResults']>0){
$firstAccountId=$accounts['items'][0]['id'];
$webproperties=$analytics->management\u webproperties->listManagementWebproperties($firstAccountId);
如果(计数($webproperties['items'])>0){
$firstWebpropertyId=$webproperties['items'][0]['id'];
$profiles=$analytics->management\u profiles->listManagementProfiles($firstAccountId,$firstWebpropertyId);
如果(计数($profiles['items]])>0){
$profileId=$profiles['items'][0]['id'];
$profileId=$pageName;
$results=$analytics->data\u ga->get(
“ga:”.$profileId,
$date\u在一个月之前,
$date_今天,
'ga:Visitions,ga:pageviews,ga:pageviewsPerVisit,ga:avgTimeOnPage,ga:visitBounceRate,ga:PercentNewVisitions,ga:uniquePageviews',
排列(
“维度”=>“ga:来源,ga:关键字,ga:浏览器,ga:日期,ga:月,ga:日,ga:年”,
'排序'=>'-ga:访问,ga:关键字',
//'过滤器'=>'ga:medium==有机',
“最大结果”=>“25”);
$visions=“”;
$unique_visitors=“”;
$pageviews=“”;
$pages_visit=“”;
$avg_vision_duration=“”;
$bounce_rate=“”;
$new_visions=“”;
echo“
”$visions=$results['totalsForAllResults']['ga:visions']; echo“
”$unique_visitors=$results['totalsForAllResults']['ga:uniquePageviews']; echo“
”$pageviews=$results['totalsForAllResults']['ga:pageviews']; $pages_visit=$results['totalsForAllResults']['ga:pageviewsPerVisit']; $avg_访问时间=$results['totalsForAllResults']['ga:avgTimeOnPage']; $bounce_rate=$results['totalsForAllResults']['ga:visitBounceRate']; $new_visions=$results['totalsForAllResults']['ga:percentnewvisions']; $profileName=$results['profileInfo']['profileName']; }else{打印“未找到此用户的配置文件”。;} }else{打印“未找到此用户的webproperties”。;}//如果条件满足,请关闭webproperties结果 }else{打印“未找到此用户的帐户”。;}//如果条件满足,则关闭总结果 } }
错误:
致命错误:在E:\xampp\htdocs\DATAPOINTS\apigp\io\Google#REST.php:66堆栈跟踪:#0 E:\xampp\htdocs\DATAPOINTS\apigp\io\Google#REST.php(36):Google#REST::decodehttpsresponse(对象(Google##HttpRequest))#1 E:\xampp\htdocs\DATAPOINTS\apigp\service\Google_ServiceResource.php(186):Google_REST::execute(Object(Google_HttpRequest))#2 E:\xampp\htdocs\DATAPOINTS\apigp\contrib\Google_AnalyticsService.php(139):Google_ServiceResource->u调用('list',数组)#3 E:\xampp\htdocs\DATAPOINTS\anatest.php(33):Google#u ManagementAccountsServiceResource->listManagementAccounts()#4{main}在第66行的E:\xampp\htdocs\DATAPOINTS\apigp\io\Google#u REST.php中抛出


如何在不登录api的情况下获取分析数据?

根据谷歌的说法,您必须在每个api请求中发送授权凭证


请参见此处:

我可以通过以下代码获得google plus凭据:$plus=新的google_plus服务($google)$数据=$plus->people->get('10593270087874xxxxxxx');我想做同样的事情来获得分析数据。怎么做?