How do I access the Google Spreadsheets API in PHP

2019-01-18 10:43发布

问题:

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.

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.

回答1:

You can use the PHP Google Spreadsheet Client library.

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



回答2:

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.

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

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 list in the url with cell)

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



回答3:

(Aug 2016) 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 newer Google APIs do not use 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 PHP as others have already stated. The latest API is much more powerful and flexible than any previous API. Here's one code sample to help get you started. If you're not "allergic" to Python, I also made a video with a different, slightly longer example introducing the API and gave a deeper dive into its code via a blogpost that you can learn from.

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 API instead.