如何在 PHP 中访问 Google 电子表格 API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20407579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:10:25  来源:igfitidea点击:

How do I access the Google Spreadsheets API in PHP?

phpgoogle-apigoogle-api-php-clientgoogle-sheets-api

提问by Jo Sprague

Starting with the Google Developer documentation on the Google Spreadsheets API, I found that "A number of client libraries are provided in various languages.", but Google only provides client libraries for Java and .NET. For a PHP library, they recommend using Zend GData. So, I headed over the the Zend GData repo, and Zend says that their GData component is discontinued, and to use Google APIs Client Library for PHP. That library doesn't work with the Spreadsheets API, for that you have to go back to the original page that I started with.

Google Spreadsheets API 上的Google Developer文档开始,我发现“以各种语言提供了许多客户端库。”,但Google 仅提供Java 和.NET 的客户端库。对于 PHP 库,他们推荐使用 Zend GData。因此,我浏览了Zend GData 存储库,Zend 说他们的 GData 组件已停产,并使用Google APIs Client Library for PHP。该库不适用于电子表格 API,因此您必须返回到我开始使用的原始页面。

So, it seems like everyone's passing the buck. I just need to write some PHP code that requests and processes data from a Google Spreadsheet. How do I do that? Please don't link me to similar posts or pages without checking to be sure that they are 100% up to date, I've been sifting through a huge mess of posts for hours that are all full of outdated and deprecated dependencies.

所以,似乎每个人都在推卸责任。我只需要编写一些 PHP 代码来请求和处理来自 Google 电子表格的数据。我怎么做?请不要在没有检查以确保它们是 100% 最新的情况下将我链接到类似的帖子或页面,我几个小时以来一直在筛选一大堆帖子,这些帖子都充满了过时和弃用的依赖项。

回答by aman

You can use the PHP Google Spreadsheet Client library.

您可以使用PHP Google 电子表格客户端库

You'll need to use the Google APIs Client Library for PHPas well to authenticate via OAuth2. Developer documentation is linked on the github page.

您还需要使用适用于 PHPGoogle API 客户端库来通过 OAuth2 进行身份验证。开发人员文档链接在 github 页面上。

回答by apadana

Thanks for the hints and links on this page, I wanted to share what I ended up doing to read a google spreadsheet in php. You can access the spreadsheet in json format, and don't need to use api or zend framework or gdata library. PhP can handle json very easily and it was the most neat solution I could reach to because it has no dependency to any third party libraries.

感谢此页面上的提示和链接,我想分享我最终在 php 中阅读谷歌电子表格的过程。您可以访问json格式的电子表格,不需要使用api或zend框架或gdata库。PhP 可以非常轻松地处理 json,这是我能想到的最简洁的解决方案,因为它不依赖于任何第三方库。

Here is a sample link for getting a spreadsheet in json format.

这是获取 json 格式电子表格的示例链接。

https://spreadsheets.google.com/feeds/list/key/1/public/full?alt=json

Please note if spreadsheet is private you still need to follow the authentication steps to get token to access to the page. In my case the spreadsheet was public.

请注意,如果电子表格是私有的,您仍然需要按照身份验证步骤获取访问该页面的令牌。就我而言,电子表格是公开的。

Also you can access the spreadsheet via feed list or cell based (replace the listin the url with cell)

您也可以通过提要列表或基于单元格访问电子表格(将 url 中的列表替换为单元格

You can try the link in a browser and see the json result.

您可以在浏览器中尝试链接并查看 json 结果。

回答by wescpy

(Aug 2016)

(2016 年 8 月)

As of this past May, you now have a better solution...

截至今年五月,您现在有了更好的解决方案......

No more passing the buck:

不再推卸责任:

  1. GData APIs(and thus Zend GData) are the previous generation of Google APIs. While not all GData APIs have been deprecated, all newerGoogle APIsdo notuse the Google Data protocol.
  2. There is a "new" Google Sheets API v4(not GData]). In order to use the new Sheets API, you need to get the Google APIs Client Library for PHPas others have already stated. The latest API is much more powerful and flexible than any previous API. Here's one code sampleto help get you started. If you're not "allergic" to Python, I also made a videowith a different, slightly longer example introducing the API and gave a deeper dive into itscode via a blogpostthat you can learn from.
  1. GData API(以及 Zend GData)是上一代 Google API。虽然不是所有的GData API与已被否决,所有更新的谷歌的API没有使用谷歌数据协议
  2. 有一个“新的”Google Sheets API v4(不是 GData])。为了使用新的Sheets API,您需要像其他人所说的那样获取适用于 PHP 的 Google API 客户端库。最新的 API 比任何以前的 API 都更加强大和灵活。这是帮助您入门的一个代码示例。如果您对 Python 不“过敏”,我还制作了一个视频,其中包含一个不同的、稍长的示例介绍 API,并通过您可以学习的博客文章更深入地了解代码。

Note the Sheets API allows you to create spreadsheets & sheets, upload & download data, as well as, in the general sense, programmatically access a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.), but to perform file-level access such as uploads & downloads, imports & exports (same as uploads & downloads but conversion to/from Google Apps formats), you would use the Google Drive APIinstead.

请注意,Sheets API 允许您创建电子表格和工作表、上传和下载数据,以及在一般意义上以编程方式访问工作表,就像您在使用用户界面一样(创建冻结行、执行单元格格式设置、调整行大小/列、添加数据透视表、创建图表等),但要执行文件级访问,例如上传和下载、导入和导出(与上传和下载相同,但与 Google Apps 格式之间的转换),您将使用Google改为驱动 API