通过Google Drive PHP库API V3将文件从Google驱动器复制到我们的服务器

时间:2017-02-08 15:17:11

标签: php google-api-php-client google-drive-realtime-api

我正在尝试将Google云端硬盘整合到我们的网站中。我们想要做的是将文件从Drive复制到我们的服务器,你知道,有点:发送到我的文件。当我尝试整合时,我面临以下错误

enter image description here

我尝试过的代码:

require_once(ROOT . DS . 'vendor' . DS . 'google-api-php-client' . DS . 'vendor' . DS . 'autoload.php');

        define('APPLICATION_NAME', 'Drive API PHP Quickstart');
        define('CREDENTIALS_PATH', ROOT . DS . 'vendor' . DS. 'google-api-php-client' . DS .'vendor' . DS . 'drive-php-quickstart.json');
        define('CLIENT_SECRET_PATH', ROOT . DS . 'vendor' . DS. 'google-api-php-client' . DS . 'vendor' . DS . 'client_secret.json');
        define('SCOPES', implode(' ', array(\Google_Service_Drive::DRIVE_METADATA_READONLY)));


        $client = new \Google_Client();
        $client->setApplicationName(APPLICATION_NAME);
        $client->setScopes(SCOPES);
        $client->setAuthConfig(CLIENT_SECRET_PATH);
        $client->setAccessType('offline');

        // Load previously authorized credentials from a file.
        $credentialsPath = $this->expandHomeDirectory(CREDENTIALS_PATH);
        if (file_exists($credentialsPath)) {
            $accessToken = json_decode(file_get_contents($credentialsPath), true);
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

            // Store the credentials to disk.
            if (!file_exists(dirname($credentialsPath))) {
                mkdir(dirname($credentialsPath), 0700, true);
            }
            file_put_contents($credentialsPath, json_encode($accessToken));
            printf("Credentials saved to %s\n", $credentialsPath);
        }
        $client->setAccessToken($accessToken);


        // Refresh the token if it's expired.
        if ($client->isAccessTokenExpired()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
        }

1 个答案:

答案 0 :(得分:0)

你错误Notice: Use of undefined constant STDIN意味着它正在寻找名为STDIN的常量。当它没有找到这样的常量时,PHP会将其解释为字符串。显然,如果你以后定义了这样的常量,这很容易破坏。它声明here您应引用数组键以避免此类错误。错误消息是由于PHP将隐式声明未知标记为同名的常量字符串而导致的错误消息。您也可以查看以下链接:Copy a file from Google Drive to my own server