如何阅读PHP中的Google

如何阅读PHP中的Google

本文介绍了如何阅读PHP中的Google Drive电子表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的是从网站上阅读Google Spreadsheet。我已阅读并重新阅读Google Drive API文档以及Stack Overflow上的所有Google Drive PHP,但仍无法到达结束区域。



以下是我已经完成:


  1. 去过Google API控制台并且:


    1. 已启用服务下的Drive API和Drive SDK;
    2. 在API访问下创建了OAuth 2.0客户端ID。在Web应用程序的客户端ID下,控制台给了我客户端ID,电子邮件地址,客户端秘密,重定向URI和JavaScript起源;


  2. 下载Google API PHP客户端库;
  3. 打开Goog​​le Drive文档(电子表格)并点击分享获取文档'key';

  4. 设置以下代码:



 <?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'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));

尝试{
$ file = $ service-> files-> get(GDRIVE_FILE_KEY);
echoTitle:,$ file-> getTitle();
echoDescription:,$ file-> getDescription();
echoMIME type:,$ file-> getMimeType();
} catch(Exception $ e){
echo发生了一个错误:,$ e-> getMessage();
}
?>

直到 $ service->都可以正常运行(无错误)

我做错了什么?我已经把我的头发拉出来了(好吧,剩下的东西)。

解决方案

解决方案,如果您不想使用API​​或Google身份验证。


  1. 转到Google云端硬盘中的电子表格。
  2. >
  3. 转至文件 - >在网络上发布

  4. 选择正确的工作表,并确保启用复选框始终更新。

  5. 仍然在同一个框中,您可以获得链接到电子表格的.csv链接。 现在可以像任何Web上的其他csv文件。
    以下是一些示例代码:

      $ spreadsheet_url =https://docs.google.com/spreadsheet/酒馆键= LT; somecode>&安培;单=真安培; GID = 0&安培;输出= CSV; $!
    $ b if(!ini_set('default_socket_timeout',15))echo<! - 无法更改套接字超时 - >; (($ $ = $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' ))!== FALSE){
    $ spreadsheet_data [] = $ data;
    }
    fclose($ handle);
    }
    else
    die(Problem reading csv);


    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.

    Here's what I've done :

    1. Been to the Google APIs Console and :

      1. Enabled "Drive API" and "Drive SDK" under 'Services';
      2. Created an OAuth 2.0 client ID under 'API Access'. Under "Client ID for web applications", the console gave me "Client ID", "Email address", "Client secret", "Redirect URIs" and "JavaScript origins";

    2. Downloaded the "Google API PHP Client Library";
    3. Opened the Google Drive document (spreadsheet) and clicked on "Share" to get the document's 'key';
    4. Set up the following code :
    <?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();
    }
    ?>
    

    All runs fine (no errors anyway) until the $service->files->get( GDRIVE_FILE_KEY ) call which triggers the exception:

    What am I doing wrong? I've pulled my hair out (well, what was left).

    解决方案

    There is also a much easier, but less clean solution, if you don't want to bother with the API or Google Authentication.

    1. Go to your Spreadsheet in Google Drive.
    2. Go to files -> Publish on the Web
    3. Select the right worksheet and make sure to enable the checkbox always update.
    4. Still in the same box you can get a .csv Link to your spreadsheet.

    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电子表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:38