You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地PHP应用调用Google API遇ConnectException连接拒绝问题求助

Google API登录本地环境连接拒绝问题排查与解决

问题场景

本地开发环境(localhost)使用PHP 8.1.20、Composer 2.5.8及google/apiclient 2.15.0实现Google API用户登录,在Firefox浏览器中触发以下致命错误:

Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: Connection refused for URI https://www.googleapis.com/oauth2/v3/certs
in C:\UwAmp\www\mysite\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php:329
...

对应的登录PHP代码:

<?php
// print_r($_COOKIE);
// print_r($_POST);
if (!empty($_POST['credential'])) {
    if (empty($_COOKIE['g_csrf_token']) || empty($_COOKIE['g_csrf_token']) || $_COOKIE['g_csrf_token'] != $_POST['g_csrf_token']) {
        echo "Erreur vérification jeton CSRF";
        exit();
    }

    $clientID = "numbers.apps.googleusercontent.com";
    $client = new Google_Client(['client_id' => $clientID]);  // Specify the CLIENT_ID of the app that accesses the backend

    $idToken = $_POST['credential'];
    $user = $client->verifyIdToken($idToken);
    // print_r($user);
    if ($user) {
        $_SESSION['user'] = $user;
        header("Location: profil.php");
        exit();
    } else {
        echo "Erreur lors de l'authentification";
    }
}

(注:代码注释为法语)

排查与解决步骤

1. 检查本地网络与代理设置

  • 直接在浏览器访问https://www.googleapis.com/oauth2/v3/certs,若无法打开,说明本地网络无法连接Google服务,需检查防火墙规则、DNS配置或切换网络环境。
  • 若使用代理,需在Google Client中配置代理信息:
    $client = new Google_Client(['client_id' => $clientID]);
    $client->setHttpClient(new GuzzleHttp\Client([
        'proxy' => 'http://your-proxy-host:port', // 替换为实际代理地址
    ]));
    

2. 验证PHP扩展与SSL配置

  • 打开php.ini,确保extension=openssl未被注释,重启UwAmp服务。
  • 配置PHP的CA证书路径:在php.ini中设置curl.cainfo = "path/to/cacert.pem"(可下载官方最新CA证书文件),避免SSL证书验证失败。

3. 更新Google API Client版本

当前使用的2.15.0版本较旧,可能存在PHP 8.1兼容性问题:

  • 执行Composer命令更新至最新稳定版:
    composer update google/apiclient
    

4. 临时禁用SSL验证(仅开发环境)

若为本地SSL证书验证问题,可临时关闭Guzzle的SSL检查(生产环境禁止使用):

$client = new Google_Client(['client_id' => $clientID]);
$httpClient = new GuzzleHttp\Client([
    'verify' => false,
]);
$client->setHttpClient($httpClient);

5. 检查UwAmp服务配置

  • 确认UwAmp的Apache/Nginx服务未被防火墙拦截,允许对外发起网络请求。
  • 尝试切换UwAmp使用的PHP版本至8.0.x,排查PHP 8.1的兼容性问题。

内容的提问来源于stack exchange,提问作者Leo_Clipet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 23:23:12