2016-05-24 15 views
0

Ich implementiere aus einigen Gründen eine Datei, die mit php curl hochlädt. Aber ich kann keine Datei hochladen und false Antwort bekommen. ich referierte Google Drive Document. Irgendeine Idee?Dateien mit php curl in Google Drive hochladen

$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

Mögliches Duplikat von http://stackoverflow.com/questions/13483846/curl-upload-to-google-drive –

Antwort

1

Ich konnte Dateien hochladen, indem ich CURLOPT_POSTFIELDS-Parameter in Array-Daten änderte.

$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; 
Verwandte Themen