2015-04-30 77 views
0

您好,我在使用PHP Client Library致電Google Drive API時遇到問題。將文件上傳到Google Drive時未定義的變量

我的編碼是

$file = new Google_Service_Drive_DriveFile(); 
$file->setTitle($title); 
$result = $service->files->insert($file, array(
'data' => file_get_contents("dd.doc"), 
'mimeType' => 'text/plain', 
'uploadType' => 'multipart' 
)); 

我得到了以下錯誤

Undefined variable: service Trying to get property of non-object Call to a member function insert()

+0

哪裏$服務初始化的一部分? –

+0

尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建最小,完整和可驗證示例。 – DaImTo

回答

0

是否包含在腳本中的客戶端的lib?還是你忘了添加那部分?

require_once 'Google/Client.php';  
require_once 'Google/Service/Drive.php'; 

還檢查$服務是否正確創建。你錯過了那段代碼。

$client_id = '<YOUR_CLIENT_ID>'; 
$client_secret = '<YOUR_CLIENT_SECRET>'; 
$redirect_uri = '<YOUR_REDIRECT_URI>'; 
$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); 

一個很好的例子可以發現here

+0

已經打電話給那個文件 – Athi

+0

你在哪裏創建$ service,你應該真的發佈你所有的代碼,但是沒有足夠的幫助你。 – DaImTo

+1

我現在已經初始化了,但是我得到了像致命錯誤一樣的錯誤:Uncaught異常'Google_Service_Exception'帶有消息'Error – Athi

相關問題