首页 > 解决方案 > 使用 php 在 google sheet 上附加数据适用于本地主机,但不适用于 Cpanel

问题描述

我能够让我的代码使用 Google API 将表单提交的数据附加到 google sheet 文档

<?php
    require __DIR__ . '/vendor/autoload.php';

    $client = new \Google_Client();
    $client->setApplicationName('php to sheets');
    $client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
    $client->setAccessType('offline');
    $client->setAuthConfig(__DIR__ . '/credentials.json');

    $service = new Google_Service_Sheets($client);
    $spreadsheetId = "1QB_xjhw18b7J7WG0BSYkjf9RgqqgZspz_KYmBqD9yS4";

    if (!empty($_POST)){
        echo $_POST['name'];

        $range = "congress";
        $values = [
            [$_POST['name'] , $_POST['rating'] , $_POST['notes'] ],
        ];

        $body = new Google_Service_Sheets_ValueRange([
            'values' => $values
        ]);

        $params = [
            'valueInputOption' => 'RAW'
        ];

        $insert = [
            "insertDataOption" => "INSERT_ROWS"
        ];

        $result = $service->spreadsheets_values->append(
            $spreadsheetId,
            $range,
            $body,
            $params,
            $insert
        );

        header("Location: Thanks.html");
    }
?>

一旦我上传了项目文件夹,页面就会卡在文件上并且不会更新谷歌表

标签: phphtmlapigoogle-sheets

解决方案


推荐阅读