php - 服务应用程序和 Google Analytics API V3:错误 101 (net::ERR_CONNECTION_RESET)

标签 php google-analytics-api

我正在尝试使用 PHP 获取我的 Google Analytics 帐户的一些信息。我已经按照在 Google 控制台 API 中创建服务帐户的步骤进行操作 in this answer .我正在使用 Google API Client for PHP .

这是我目前得到的代码:

<?php
$path_to_src = 'src';

// These files are in /src, upload its contents to your web server
require_once $path_to_src . '/Google_Client.php';               
require_once $path_to_src . '/contrib/Google_AnalyticsService.php';

$path_to_keyfile = '***'; //my private key


// Initialise the Google Client object
$client = new Google_Client();

// Your 'Product name'
$client->setApplicationName('My App Name');

$client->setAssertionCredentials(
    new Google_AssertionCredentials(        
        '**', //gserviceaccount mail
        array('https://www.googleapis.com/auth/analytics.readonly'),
        file_get_contents($path_to_keyfile)
    )
);

// Get this from the Google Console, API Access page
$client->setClientId('***'); // my cliente ID
$client->setAccessType('offline_access');
$analytics = new Google_AnalyticsService($client);

// create service and get data
$service = new Google_AnalyticsService($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'ga:****'; // my profile id
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id, 
                        $lastWeek, 
                        $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

我在 PHP 中启用了 openssl 扩展:

enter image description here

当浏览到 php 脚本的位置时,我几乎永远加载和以下错误:

enter image description here

我正在使用 PHP 5.4.7:

enter image description here

调试 Google API 客户端代码后,脚本似乎在这一行中断:

if (!openssl_sign($data, $signature, $this->privateKey, "sha256"))

此行以下的任何内容都不会被调用。看起来错误发生在这一行。是不是这里不兼容,还是什么?

最佳答案

初学者应该改变的一件事:

您将 AnalyticsService 实例化两次。取出你不用的那个:

$service = new Google_AnalyticsService($client);

看看这是否对您的问题有帮助。

关于php - 服务应用程序和 Google Analytics API V3:错误 101 (net::ERR_CONNECTION_RESET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13465689/

相关文章:

php - 空 X 零问题

javascript - 使用 href onclick 更新 div 而无需重新加载页面?

php - 如何在 YouTube 上上传大文件

google-analytics-api - 如何通过Query API获取 "entered"、 "proceeded"等Goal Funnel Step数据?

google-analytics - 遗传算法 : How to access Cohort Analysis via Analytics API?

facebook - 如何在同一帐户和 Assets 名称下跟踪 2 个不同的谷歌分析配置文件(主要和测试)?

.net - 通过 OAuth 2.0 自动使用 google-api-dotnet-client

php - mysqli 更新不起作用

php - 没有显示带有 header ('Content-type: image/png' 的图像)

php - Google Analytics API v3如何提取值?