2016-03-07 17 views
5

कॉल करने वाली अपरिभाषित विधि Google's page में प्रदान किए गए उदाहरण के आधार पर, मैंने संगीतकार के माध्यम से Google क्लाइंट एसडीके स्थापित किया। और Google डिस्क पर एक फ़ाइल अपलोड करने निम्नलिखित कोड का उपयोग करने की कोशिश कर:Google ड्राइव क्लाइंट

<?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); 
} 
?> 

insertFile() समारोह उदाहरण पृष्ठ से कॉपी किया जाता है, लेकिन जब मैं स्क्रिप्ट चलाने के लिए, यह पता चलता है:

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

है सेटअप के दौरान मुझे कुछ याद आया?

उत्तर

7
इसके बजाय $file->setTitle($title); उपयोग का उपयोग करने का

$file->setName($filename);

संपादित 2

Insert() विधि create() द्वारा प्रतिस्थापित किया जा रहा है,

Use create() instead of insert()

तो मूल रूप से नए कोड (परीक्षण नहीं किया जाएगा),

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

क्या आप कह रहे हैं कि उदाहरण अपडेट नहीं किया गया है/लाइब्रेरी अपडेट की गई है? मुझे अद्यतन उदाहरण कहां मिल सकता है? यह काम करता है, लेकिन अगली त्रुटि प्रकट होती है: ' घातक त्रुटि: अपरिभाषित विधि पर कॉल करें Google_Service_Drive_Files_Resource :: प्रविष्टि()' पंक्ति में $ $ बनाया गया फ़ाइल = $ सेवा-> फ़ाइलें-> सम्मिलित करें() ' – Raptor

+1

सम्मिलित करें() मेहोद को प्रतिस्थापित किया जा रहा है बनाएं(), आप इसे यहां पा सकते हैं https://developers.google.com/drive/v3/reference/files/create#request –

+0

आकर्षण की तरह काम करता है। धन्यवाद दोस्त। – Raptor

संबंधित मुद्दे