我正在与一个google php-client做斗争,我试图发送到谷歌驱动器指定的文件夹,我的data.xml和这段代码,它完美地工作。但是google驱动器上创建的文件始终是空的/0字节/。
<?php
require_once 'vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=../html/my-key.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$client->setSubject('my-service@account.com');
$service = new Google_Service_Drive($client);
$data = file_get_contents("data.xml");
$fileMetadata = new Google_Service_Drive_DriveFile(
array('name' => 'data',
'data' => $data,
'mimeType' => 'text/xml',
'parents' => array('myFolderID'),
'uploadType' => 'media'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));我的data.xml文件:
<?xml version="1.0"?>
<data>
<item>
<id>2546485</id>
<c>0</c>
<p>0,00</p>
</item>
<item>
<id>2546809</id>
<c>0</c>
<p>0,00</p>
</item>
<item>
<id>10982762</id>
<c>30</c>
<p>4,26</p>
</item>
</data>到目前为止,我已经尝试过改变mimeTypes和uploadTypes而没有运气。File_get_contents正确地返回.xml数据。
谢谢你提前帮忙。
发布于 2019-12-03 07:25:02
如果我的理解是正确的,这次修改怎么样?
请按以下方式修改$fileMetadata = new Google_Service_Drive_DriveFile(array())和$file = $service->files->create($fileMetadata, array())。
修改脚本:
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'data',
'parents' => array('myFolderID'),
));
$file = $service->files->create($fileMetadata, array(
'data' => $data,
'mimeType' => 'text/xml',
'uploadType' => 'media',
'fields' => 'id',
));uploadType也可以使用multipart。参考文献:
如果这不是你想要的方向,我道歉。
https://stackoverflow.com/questions/59151588
复制相似问题