google/apiclient Google_Client 与 php8 的兼容性错误:致命错误:未捕获的 TypeError:count() / 已弃用:Google_Model 的返回类型

问题描述 投票:0回答:1

运行

composer require google/apiclient
(导致
Using version ^2.0 for google/apiclient
)后,我正在使用 Google_Client 来管理使用 php8 的登录,但我遇到了不同版本 php 的兼容性错误:

php8.0 我明白了

Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in C:\wamp64\www\.....\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 67

php8.1.13 和 8.2.0 我明白

Deprecated: Return type of Google_Model::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\wamp64\www\....\vendor\google\apiclient\src\Google\Model.php on line 249

google/apiclient 声称是“PHP7.4 或更高版本”,但一旦检测到

$_POST['credential']
,显然就会遇到这些问题。

我在开发过程中从 Windows 11 PC 上的 wamp 本地主机运行此程序。

我需要做什么才能让 googleapi 登录正常工作?

index.php:

<html lang="en">
<body>
<?php
require_once 'class.GoogleLogin.php';
$google=new GoogleLogin();
$google->handleGooglePosts();
echo $google->getGoogleScriptInclude();

class.GoogleLogin.php

<?php
require_once 'class.config.php';

class GoogleLogin {

    public function getGoogleScriptInclude() {
        return "<script src='https://accounts.google.com/gsi/client' async defer></script>";
    }
    //--------------------------------------------------------------------------------
    public function getGoogleLoginTags() {
        $redirect=config::google_redirectUri;

        return "
                <div id='g_id_onload'
                    data-client_id='".config::google_clientId."'
                    data-login_uri='$redirect'
                    data-auto_prompt='false'>
                </div>
                <div class='g_id_signin'
                    data-type='standard'
                    data-size='large'
                    data-theme='outline'
                    data-text='sign_in_with'
                    data-shape='rectangular'
                    data-logo_alignment='left'>
                </div>
            ";
    }
    //--------------------------------------------------------------------------------
    Final public function handleGooglePosts() {
        $site=config::siteURL;
        $redirect=$_SERVER['REQUEST_URI'];
        if ( isset($_POST['credential'])) {
            $id_token = $_POST['credential'];

            require_once 'autoload.php';
            $client = new Google_Client(['client_id' => config::google_clientId]);

            if ( !$client ) {
                echo "<LI>No client";exit;
            }

            $guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false,),));
            $client->setHttpClient($guzzleClient);

            try {
                $payload = $client->verifyIdToken($id_token);
            }
            catch (Exception $e) {
                echo "<LI>Exception ".$e->getMessage();
                exit;
            }
            .
            .
            exit;
php google-api-client
1个回答
0
投票

嗯...与吟游诗人交谈 - 它建议我删除我的

composer.lock
文件并重新发布
composer require google/apiclient
。这似乎现在有效(我认为)

© www.soinside.com 2019 - 2024. All rights reserved.