簡體   English   中英

Google Api PHP客戶端庫

[英]Google Api PHP Client Library

我正在嘗試將Google API PHP客戶端庫用於Google Analytic v3。

我可以運行我在家里寫的簡單應用程序,但是當我在辦公室試用它時它不起作用。 當我運行程序時,我被要求將php應用程序授權給我的谷歌帳戶。 允許訪問后我得到了

Google_IOException:HTTP錯誤:(0)無法連接到第128行的C:\\ wamp \\ www \\ google \\ GoogleClientApi \\ io \\ Google_CurlIO.php中的主機

必須連接到我的組織的代理服務器。 有誰知道如何使用oauth 2和php客戶端庫連接到代理服務器。

謝謝

下面是我的php客戶端的代碼。

session_start();
require_once dirname(__FILE__).'/GoogleClientApi/Google_Client.php';
require_once dirname(__FILE__).'/GoogleClientApi/contrib/Google_AnalyticsService.php';

$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('My Application name');
//$client->setClientId(''); omitted for privacy
//$client->setClientSecret(''); omitted for privacy
$client->setRedirectUri($scriptUri);
//$client->setDeveloperKey(''); // API key omitted for privacy

// $service implements the client interface, has to be set before auth call
$service = new Google_AnalyticsService($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
die('Logged out.');
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token     and store it in session
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}

echo 'Hello, world.';

只是添加(因為我無法在谷歌中找到任何結果)如果你想避免編輯庫本身你可以通過$ client對象指定額外的curl參數。 這樣做的代碼看起來大致如此。

$client = new Google_Client();
$client->getIo()->setOptions(array(
    CURLOPT_PROXY => 'myproxy.mywebsite.com',
    CURLOPT_PROXYPORT => 8909
));

您必須在curl中配置代理設置。 檢查Google_CurlIO.php是否有一行調用curl_exec($ch)

您可能需要事先添加類似於:

curl_setopt($ ch,CURLOPT_PROXY,'your-proxy-server');

v2.0.0更新

$client = new Google_Client();
$httpClient = $client->getHttpClient();
$httpClient->setDefaultOption("proxy", "http://{$proxyUser}:{$proxyPass}@{$proxyAddress}:{$proxyPort}");

版本2.2.0的更新

該庫使用Guzzle讀取環境變量以自動設置(或不設置)代理(參見GuzzleHttp \\ Client類)第177行:

    if ($proxy = getenv('HTTPS_PROXY')) {
        $defaults['proxy']['https'] = $proxy;
    }

我假設您需要一個HTTPS代理,因為Google OAuth無法通過簡單的HTTP工作。

只需添加

運行putenv( 'HTTPS_PROXY = HTTPS:// {your.proxy.url}:{端口號}');

它本身就可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM