服务应用程序和Google AnalyticsAPI V3:错误101(net :: ERR_CONNECTION_RESET)

时间:2012-11-20 02:21:28

标签: 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"))

此行以下的任何内容都不会被调用。看起来错误发生在这一行。这里有不兼容的东西吗?

1 个答案:

答案 0 :(得分:2)

首先应该改变一下:

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

$service = new Google_AnalyticsService($client);

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