Google Drive HTTP上传“未找到”问题PHP

时间:2012-08-19 05:42:40

标签: php file upload google-drive-api

我正在尝试使用http标头和CURL帖子将文件上传到Google驱动器,并且我从谷歌错误中返回“未找到”。 我认为这是因为我通过CURL上传文件的方式,因为我从未做过。 这是我的代码:

$file = file_get_contents("./ima.jpg");
$length = strlen($file);
test($file,$length);

function test($file,$length){
$url2="https://www.googleapis.com/upload/drive/v2/filesuploadType=media";
$header = array(
"Content-Type: image/jpeg",
"Content-Length:$length ",
"Authorization: Bearer $token",
);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url2);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);
curl_setopt ($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$file);
$data2 = curl_exec($ch);
echo $data2;
curl_close($ch);
}

令牌设置在一个变量令牌中,它是一个有效的令牌,因为它适用于谷歌驱动器列出文件,谢谢!

1 个答案:

答案 0 :(得分:2)

uploadType是网址的参数,需要使用?分隔,这意味着在您的情况下,网址最有可能是;

$url2="https://www.googleapis.com/upload/drive/v2/files?uploadType=media";

有关更详细的文档,请参阅here