由于某些原因,我正在使用php curl实现文件上传。
但我无法上传文件并获得false
响应。
我提到details。
有什么想法吗?
$token = "xxxx";
$url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart";
$inputarray = '';
$inputarray .= "--foo_bar_baz\r\n";
$inputarray .= "Content-Type: application/json; charset=UTF-8\r\n\r\n";
$inputarray .= "{\r\n";
$inputarray .= "\"name\": \"upload.jpg\"\r\n";
$inputarray .= "}\r\n\r\n";
$inputarray .= "--foo_bar_baz\r\n";
$inputarray .= "Content-Type:image/jpeg\r\n\r\n";
$inputarray .= file_get_contents("upload.jpg") . "\r\n";
$inputarray .= "--foo_bar_baz--\r\n";
$headers = array(
'Content-Type: multipart/related; boundary=foo_bar_baz',
'Authorization: Bearer ' . $token,
'Content-Length: ' . strlen($inputarray)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $inputarray);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
答案 0 :(得分:1)
我可以通过将CURLOPT_POSTFIELDS参数更改为数组数据来上传文件。
$token = "xxx";
$url = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart";
$headers = array(
"Content-Type:multipart/related",
"Authorization: Bearer ".$token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$data1 = '@'.realpath('./metadata.json').';type=application/json;charset=UTF-8';
$data2 = '@'.realpath('./test.csv').';type=text/csv';
$postdata = array(
'metadata'=>$data1,
'file'=>$data2
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$output = curl_exec($ch);
curl_close($ch);
echo $output;