2015-04-30 79 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

相关问题