How do I read a Google Drive Spreadsheet in PHP?(如何在 PHP 中阅读 Google Drive 电子表格?)
问题描述
我要做的只是从网站上读取 Google 电子表格.我已经阅读并重新阅读了 Google Drive API 文档以及 Stack Overflow 上的所有 Google Drive PHP 内容,但我仍然无法到达终点.
All I'm trying to do is read a Google Spreadsheet from a web site. I've read and re-read the Google Drive API docs and everything Google Drive PHP on Stack Overflow and I still can't get to the end zone.
这是我所做的:
- 访问过 Google API 控制台并:
- Been to the Google APIs Console and :
- 在服务"下启用Drive API"和Drive SDK";
- 在API 访问"下创建了 OAuth 2.0 客户端 ID.在Web 应用程序的客户端 ID"下,控制台给了我客户端 ID"、电子邮件地址"、客户端密码"、重定向 URI"和JavaScript 来源";
<?php
session_start();
require_once 'lib/gapi/Google_Client.php';
require_once 'lib/gapi/contrib/Google_DriveService.php';
define( 'GDRIVE_CLIENT_ID', '<API Console - API Access - Client ID>' );
define( 'GDRIVE_CLIENT_SECRET', '<API Console - API Access - Client secret>' );
define( 'GDRIVE_REDIRECT_URIS', '<API Console - API Access - Redirect URIs>' );
define( 'GDRIVE_SCOPE_01', 'h t t p s://www.googleapis.com/auth/drive' );
define( 'GDRIVE_SCOPE_02', 'h t t p s://www.googleapis.com/auth/drive.apps.readonly' );
define( 'GDRIVE_SCOPE_03', 'h t t p s://www.googleapis.com/auth/drive.file' );
define( 'GDRIVE_SCOPE_04', 'h t t p s://www.googleapis.com/auth/drive.metadata.readonly' );
define( 'GDRIVE_SCOPE_05', 'h t t p s://www.googleapis.com/auth/drive.readonly' );
define( 'GDRIVE_FILE_KEY', '<'key' given from 'sharing' document>' );
$client = new Google_Client();
$client->setClientId( GDRIVE_CLIENT_ID );
$client->setClientSecret( GDRIVE_CLIENT_SECRET );
$client->setRedirectUri( GDRIVE_REDIRECT_URIS );
$client->setScopes( array( GDRIVE_SCOPE_01, GDRIVE_SCOPE_02, GDRIVE_SCOPE_03, GDRIVE_SCOPE_04, GDRIVE_SCOPE_05 ) );
try {
$file = $service->files->get( GDRIVE_FILE_KEY );
echo "Title: ", $file->getTitle();
echo "Description: ", $file->getDescription();
echo "MIME type: ", $file->getMimeType();
} catch (Exception $e) {
echo "An error occurred: ", $e->getMessage();
}
?>
在触发异常的 $service->files->get( GDRIVE_FILE_KEY )
调用之前,一切都运行良好(无论如何都没有错误):
All runs fine (no errors anyway) until the $service->files->get( GDRIVE_FILE_KEY )
call which triggers the exception:
发生错误:调用 GET https://www.googleapis.com/drive/v2/files
时出错:(403) 已超出未经验证使用的每日限制.继续使用需要注册.
An error occurred: Error calling GET
https://www.googleapis.com/drive/v2/files
: (403) Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
我做错了什么?我把头发拔掉了(嗯,剩下的).
What am I doing wrong? I've pulled my hair out (well, what was left).
推荐答案
如果您不想打扰 API 或 Google 身份验证,还有一个更简单但不太干净的解决方案.
There is also a much easier, but less clean solution, if you don't want to bother with the API or Google Authentication.
- 转到您在 Google 云端硬盘中的电子表格.
- 转到文件 ->发布到网络
- 在发布到网络对话框中,您可以获得指向电子表格的 .csv 链接.
- Go to your Spreadsheet in Google Drive.
- Go to File -> Publish to the Web
- In the Publish to the web dialog you can get a .csv Link to your spreadsheet.
您现在可以像访问网络上的任何其他 csv 文件一样访问内容.下面是一些示例代码:
You can now access the contents like any other csv File on the Web. Here is some sample code:
$spreadsheet_url="https://docs.google.com/spreadsheet/pub?key=<somecode>&single=true&gid=0&output=csv";
if(!ini_set('default_socket_timeout', 15)) echo "<!-- unable to change socket timeout -->";
if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$spreadsheet_data[] = $data;
}
fclose($handle);
}
else
die("Problem reading csv");
这篇关于如何在 PHP 中阅读 Google Drive 电子表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHP 中阅读 Google Drive 电子表格?
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01