无需引入vendor/autoload.php,如何通过API/CDN操作Google表格?
无需Google PHP客户端库读写Google Sheets的替代方案
当然可以,不用官方PHP客户端库(即无需vendor文件夹和autoload.php),你可以通过以下两种核心方案实现Google Sheets的读写操作:
方案1:直接调用Google Sheets REST API
绕过官方SDK,直接用PHP的cURL或file_get_contents发送HTTP请求到Google Sheets的REST API端点,是最轻量化的实现方式。
用API密钥实现只读/受限读写(适合公开表格或IP白名单场景)
如果你的表格是公开可访问,或者能限制API密钥的调用IP,直接用API密钥认证即可:
<?php // 读取表格数据示例 $spreadsheetId = "你的表格ID"; $range = "Sheet1!A1:C10"; $apiKey = "你的API密钥"; $url = "https://sheets.googleapis.com/v4/spreadsheets/{$spreadsheetId}/values/{$range}?key={$apiKey}"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); print_r($data['values']); ?>
用服务账号实现全权限读写
如果需要修改表格数据,得用服务账号的OAuth 2.0认证(生成JWT令牌),步骤如下:
- 从Google Cloud控制台下载服务账号的JSON密钥文件
- 用PHP生成JWT令牌,向Google的令牌端点换取访问令牌
- 用访问令牌调用API进行读写
示例代码(生成访问令牌+写入数据):
<?php // 服务账号JSON密钥内容(可直接写在代码里,或读取本地文件) $serviceAccount = json_decode('{ "type": "service_account", "project_id": "你的项目ID", "private_key_id": "你的私钥ID", "private_key": "你的私钥内容", "client_email": "你的服务账号邮箱", "client_id": "你的客户端ID", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "你的证书URL" }', true); // 生成JWT令牌 $header = json_encode(['alg' => 'RS256', 'typ' => 'JWT']); $payload = json_encode([ 'iss' => $serviceAccount['client_email'], 'scope' => 'https://www.googleapis.com/auth/spreadsheets', 'aud' => $serviceAccount['token_uri'], 'exp' => time() + 3600, 'iat' => time() ]); $base64Header = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header)); $base64Payload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload)); $signature = hash_hmac('sha256', $base64Header . "." . $base64Payload, $serviceAccount['private_key'], true); $base64Signature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature)); $jwt = $base64Header . "." . $base64Payload . "." . $base64Signature; // 换取访问令牌 $ch = curl_init($serviceAccount['token_uri']); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwt ])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tokenResponse = json_decode(curl_exec($ch), true); curl_close($ch); $accessToken = $tokenResponse['access_token']; // 写入数据到表格 $spreadsheetId = "你的表格ID"; $range = "Sheet1!A1"; $values = [["新数据1", "新数据2"]]; $postData = json_encode([ 'values' => $values ]); $ch = curl_init("https://sheets.googleapis.com/v4/spreadsheets/{$spreadsheetId}/values/{$range}?valueInputOption=RAW"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer {$accessToken}", "Content-Type: application/json" ]); $response = curl_exec($ch); curl_close($ch); print_r(json_decode($response, true)); ?>
方案2:使用单文件轻量第三方库
找一些仅需单个PHP文件的第三方Google Sheets封装库,这类库通常只实现核心功能,不用依赖整个Composer生态。你可以直接把单文件上传到FTP,无需vendor文件夹。
核心逻辑和方案1一致,只是把HTTP请求与认证逻辑封装成了更易用的方法,你也可以自己封装一个极简类来简化重复操作。
内容的提问来源于stack exchange,提问作者Arivazhagan E
相关产品推荐
相关产品推荐

