SQLの窓

2015年01月10日


GitHub の google-api-php-client( PHP ) を使って、Google Drive の指定フォルダ内に比較的小さいファイルをアップロードする

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の最新記事】
posted by lightbox at 2015-01-10 21:24 | Google | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり