栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Google Drive PHP API-简单文件上传

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Google Drive PHP API-简单文件上传

使用此代码来认证和上传测试文件。您需要

<YOUR_REGISTERED_REDIRECT_URI>
对此文档本身进行设置(以及在控制台中)以进行身份​​验证。

require_once 'Google/Client.php';require_once 'Google/Service/Drive.php';$client = new Google_Client();// Get your credentials from the console$client->setClientId('<YOUR_CLIENT_ID>');$client->setClientSecret('<YOUR_CLIENT_SECRET>');$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>');$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));session_start();if (isset($_GET['pre']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {    if (isset($_GET['pre'])) {        $client->authenticate($_GET['pre']);        $_SESSION['access_token'] = $client->getAccessToken();    } else        $client->setAccessToken($_SESSION['access_token']);    $service = new Google_Service_Drive($client);    //Insert a file    $file = new Google_Service_Drive_DriveFile();    $file->setName(uniqid().'.jpg');    $file->setDescription('A test document');    $file->setMimeType('image/jpeg');    $data = file_get_contents('a.jpg');    $createdFile = $service->files->create($file, array(          'data' => $data,          'mimeType' => 'image/jpeg',          'uploadType' => 'multipart'        ));    print_r($createdFile);} else {    $authUrl = $client->createAuthUrl();    header('Location: ' . $authUrl);    exit();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405437.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号