3

Google AnalyticsPHP を使用してアカウントの情報を取得しようとしています。この回答の Google Console API でサービス アカウントを作成する手順は既に実行しています。PHP 用の Google API クライアントを使用しています。

これは私がこれまでに得たコードです:

<?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();
}

opensslPHP で拡張機能を有効にしました。

ここに画像の説明を入力

PHPスクリプトの場所を参照すると、ほぼ永遠にロードされ、次のエラーが表示されます。

ここに画像の説明を入力

私はPHP 5.4.7を使用しています:

ここに画像の説明を入力

Google API クライアント コードをデバッグした後、スクリプトが次の行で壊れているようです。

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

この行より下のものは呼び出されません。この行でエラーが発生しているようです。ここに非互換性がありますか、それとも何かありますか?

4

1 に答える 1

2

まず、変更する必要があることの 1 つ:

AnalyticsService を 2 回インスタンス化します。使用していないものを取り出します。

$service = new Google_AnalyticsService($client);

それがあなたの問題にまったく役立つかどうかを確認してください。

于 2012-11-27T06:17:58.363 に答える