2016-03-07 22 views
5

Dựa trên ví dụ được cung cấp trong Google's page, tôi đã cài đặt Google Client SDK qua Composer. Và cố gắng sử dụng các mã sau để tải lên một tập tin lên Google Drive:Phương thức chưa xác định gọi cho Ứng dụng khách Google Drive

<?php 
header('Content-Type: text/plain'); 
require_once('vendor/autoload.php'); 

/** 
* Insert new file. 
* 
* @param Google_Service_Drive $service Drive API service instance. 
* @param string $title Title of the file to insert, including the extension. 
* @param string $description Description of the file to insert. 
* @param string $parentId Parent folder's ID. 
* @param string $mimeType MIME type of the file to insert. 
* @param string $filename Filename of the file to insert. 
* @return Google_Service_Drive_DriveFile The file that was inserted. NULL is 
*  returned if an API error occurred. 
*/ 
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) { 
    $file = new Google_Service_Drive_DriveFile(); 
    $file->setTitle($title); 
    $file->setDescription($description); 
    $file->setMimeType($mimeType); 

    // Set the parent folder. 
    if ($parentId != null) { 
    $parent = new Google_Service_Drive_ParentReference(); 
    $parent->setId($parentId); 
    $file->setParents(array($parent)); 
    } 

    try { 
    $data = file_get_contents($filename); 

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

    // Uncomment the following line to print the File ID 
    print 'File ID: %s' % $createdFile->getId(); 

    return $createdFile; 
    } catch (Exception $e) { 
    print "An error occurred: " . $e->getMessage(); 
    } 
} 

$client_email = '[email protected]'; 
$private_key = file_get_contents('key.p12'); 
$scopes = array(
    'https://www.googleapis.com/auth/drive.file', 
    //'https://www.googleapis.com/auth/drive.appdata', 
    //'https://www.googleapis.com/auth/drive.apps.readonly' 
); 
$credentials = new Google_Auth_AssertionCredentials(
    $client_email, 
    $scopes, 
    $private_key 
); 

$client = new Google_Client(); 
$client->setAssertionCredentials($credentials); 
if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion(); 
} 

$service = new Google_Service_Drive($client); 
// https://developers.google.com/drive/v2/reference/files/insert#examples 
$response = insertFile($service, 'Test Image for OCR', 'Image for OCR', null, 'image/jpeg', 'test1.jpg'); 
if($response === NULL) { 
    echo 'Upload failed' . PHP_EOL; 
} else { 
    var_dump($response); 
} 
?> 

Chức năng insertFile() được sao chép từ trang ví dụ, nhưng khi tôi chạy kịch bản, nó cho thấy:

Fatal error: Call to undefined method Google_Service_Drive_DriveFile::setTitle()

Is có bất cứ điều gì tôi bị mất trong khi thiết lập?

Trả lời

7

Thay vì sử dụng $file->setTitle($title); sử dụng $file->setName($filename);

Chỉnh sửa 2

Insert() phương pháp đang được thay thế bởi create(),

Use create() instead of insert()

Vì vậy, về cơ bản mã mới sẽ được (không kiểm tra),

$createdFile = $service->files->create($file, array(
     'data' => $data, 
     'mimeType' => $mimeType, 
    )); 
+0

Bạn có nói rằng ví dụ không được cập nhật/thư viện được cập nhật không? Tôi có thể tìm ví dụ được cập nhật ở đâu? Nó hoạt động, nhưng lỗi tiếp theo xuất hiện: ' Lỗi nghiêm trọng: Gọi đến phương thức không xác định Google_Service_Drive_Files_Resource :: insert()' trong dòng '$ createdFile = $ service-> files-> insert()' – Raptor

+1

Chèn() mehod đang được thay thế bằng create(), bạn có thể tìm thấy tại đây https://developers.google.com/drive/v3/reference/files/create#request –

+0

Hoạt động như sự quyến rũ. Cảm ơn người đàn ông. – Raptor

Các vấn đề liên quan