0

phpを使用してgoogledriveapiにファイルをアップロードする方法を検索しました。次の方法を使用しました

$file-setMimetype='image/jpg';
$file->setTitle='abcd'
$service->files->insert($file).

タイトルと MIME タイプを使用して googledrive にファイルを挿入しますが、空です。私の質問は、ファイルに含まれる実際のデータまたはコンテンツをアップロードする方法です。つまり、システムから googledrive に画像または pdf ファイルをアップロードする場合です。 $data で実際のデータを送信する方法について教えてください。その $data 変数には何が含まれている必要がありますか。

4

3 に答える 3

2

最近追加されたクイックスタートページに、ファイルをドライブにアップロードするための完全な PHP アプリを作成する方法に関する 10 分間のチュートリアルがあります。

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = file_get_contents('document.txt');

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));

print_r($createdFile);
?>
于 2012-08-22T20:38:11.723 に答える
0

これを試して:

$service->files->insert($file, array(
    'data' => $content,
    'mimeType' => $mimeType,        
));

ファイルを挿入する方法のこの例を見ることができます([PHP] タブをクリック)。最初にコンテンツを含むファイルをアップロードしてみてくださいtext/plain。コンテンツに追加する前に、画像データを base64 でエンコードする必要がある場合があります。

于 2012-08-22T13:43:54.167 に答える