google-api-php-client Client ID と Client secret と Redirect URIs が必要です ( APIs Console ) (リダイレクト URL は複数指定できます) アップロードしたいフォルダの ID が必要ですが、テストなので Google ドライブの管理画面の URL から取得しています。親フォルダは複数指定できる仕様になっており、いくつでも指定できるようです。
<?php /* * Copyright 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ include_once "templates/base.php"; session_start(); header( "Content-Type: text/html; Charset=utf-8" ); header( "pragma: no-cache" ); header( "Expires: Wed, 31 May 2000 14:59:58 GMT" ); header( "Cache-control: no-cache" ); require_once realpath(dirname(__FILE__) . '/../autoload.php'); /************************************************ $redirect_uri はこのスクリプトの URL ************************************************/ $client_id = ''; $client_secret = ''; $redirect_uri = 'http://localhost/gapi/examples/simplefileupload.php'; /************************************************ クライアントの作成 ************************************************/ $client = new Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); $service = new Google_Service_Drive($client); /************************************************ アクセストークンの取得プロセス ************************************************/ if (isset($_REQUEST['logout'])) { unset($_SESSION['upload_token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['upload_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { $client->setAccessToken($_SESSION['upload_token']); if ($client->isAccessTokenExpired()) { unset($_SESSION['upload_token']); } } else { $authUrl = $client->createAuthUrl(); } /************************************************ アクセストークンが取得できた時の処理 ************************************************/ if ($client->getAccessToken()) { // ファイルのインスタンスを作成 $file = new Google_Service_Drive_DriveFile(); $file->setTitle("比較的小さいファイルのアップロード"); $parent1 = new Google_Service_Drive_ParentReference(); // Google ドライブの管理画面のURL から ID を取得 $parent1->setId('0B9Jymqpro6gSVXBUb1N6RXAtVGc'); $parent2 = new Google_Service_Drive_ParentReference(); // Google ドライブの管理画面のURL から ID を取得 $parent2->setId('0B9Jymqpro6gSRWQ3a0VYUXFEc0U'); // 二つのフォルダ内から参照しますが、実体は一つで片方から変更すると、 // もう片方から見ても変更されています $file->setParents(array($parent1, $parent2)); $result = $service->files->insert( $file, array( // file_get_contents を使っているので、データは一括渡しになって // 大きいファイルで使うとメモリが大量に必要になるので、その場合 // は、examples/fileupload.php を参考にします。 'data' => file_get_contents("./simplefileupload.php"), 'mimeType' => 'text/plain', 'uploadType' => 'multipart' ) ); } echo pageHeader("File Upload - Uploading a small file"); ?> <div class="box"> <div class="request"> <?php if (isset($authUrl)) { echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; } ?> </div> <div class="shortened" style='width:100%;height:400px;'> <?php if (isset($result) && $result) { var_dump($result->title); } ?> </div> </div>
関連する記事
|
【Googleの最新記事】
- Google 共有ドライブの容量の上限について
- Google Classroom は無料の G Suite for Education アカウントが必要
- 教室と一対一のフォルダより新しく登録されたフォルダの中にあるZoom動画ファイルを該当するClassroom の コース内の該当するトピックに登録する
- Google Apps Script : 動画を添付して Classroom の指定のトピックへ課題として投稿する
- Google Classroom のテーマ画像のサイズと既存画像をテーマ画像として使用してみた手順
- Google Chrome でスマホのソースコードをごく普通に表示して利用する
- Gmail に 実行可能なファイルの拡張子を持つファイルを格納した zip 書庫は送れません
- Gmail で添付できないファイルをエクスプローラで検索する為の文字列
- jQuery で既存 table より Firebase Database のデータを作成する
- jQuery + Bootstrap(css) + mmenu : Firebase Database 参照と更新サンプル( 新規登録テンプレートより )
- jQuery + Bootstrap(css) + mmenu : Firebase Database 新規登録テンプレート
- jQuery + Bootstrap(css) + mmenu : Firebase ログインテンプレート
- Google ドライブの WEBホスティングが無くなったので、Google の Firebase をとりあえず使う方法
- Google サイト内検索の FORM 要素による設置
- ブラウザの geolocation で Google MAP に現在地を表示。ライブラリでさらに詳細情報。API の geocoder で名称・住所から Google MAP を表示して、ライブラリで..
- Google+ に投稿するテキスト内の文字列を太字(ボールド)にしたりイタリックにしたりする方法
- Google の Plus API を使って Google+ 投稿データを jQuery UI のアコーディオン(accordion)で表示する
- Google の タスク API(ToDoリスト) を使ってタスクリストとタスクのタイトルを jQuery のプラグインでメニュー化する
- Google API の中でも単純な Task API を使って、アクセストークン取得のテンプレートを整備しました
- GitHub の google-api-php-client( PHP ) を使って、Gmail でメールを送る( 添付ファイル付き )