php - Undefined method calling Google Drive Client -


based on example provided in google's page, installed google client sdk via composer. , trying use following codes upload file 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 file insert, including extension.  * @param string $description description of file insert.  * @param string $parentid parent folder's id.  * @param string $mimetype mime type of file insert.  * @param string $filename filename of file insert.  * @return google_service_drive_drivefile file inserted. null  *     returned if 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 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 following line print file id     print 'file id: %s' % $createdfile->getid();      return $createdfile;   } catch (exception $e) {     print "an error occurred: " . $e->getmessage();   } }  $client_email = 'drive-ocr-service@angelic-tracer-123606.iam.gserviceaccount.com'; $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 ocr', 'image ocr', null, 'image/jpeg', 'test1.jpg'); if($response === null) {     echo 'upload failed' . php_eol; } else {     var_dump($response); } ?> 

the insertfile() function copied example page, when run script, shows:

fatal error: call undefined method google_service_drive_drivefile::settitle()

is there missed during setup?

instead of using $file->settitle($title); use $file->setname($filename);

edit 2

insert() method being replaced create(),

use create() instead of insert()

so new code be(not tested) ,

 $createdfile = $service->files->create($file, array(       'data' => $data,       'mimetype' => $mimetype,     )); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -