2016-07-30 5 views
0

I-Datei von einem Server auf einen anderen übertragen möchten, ohne die Datei Steuerung zu verwenden und Locken Anfrage mit ..Wie Datei zum Hochladen mit PHP Curl ohne Input-Typ File Control zu senden?

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'api-token: 6a53bcbe222c490196e4b9f87ba9148c' --header 'api-secret: 6a53bcbe222c490196e4b9f87ba9148c' -F "document[][email protected]:/xampp/htdocs/project/image_name.ext" -F "document[][email protected]:/xampp/htdocs/project/image_name.ext" -F "document[][email protected]:/xampp/htdocs/project/image_name.ext" 'https://server_api_url.com 

' Die obige curl funktioniert ...

Aber ich weiß nicht, wie die Datei in pHP schreiben ... im Folgenden finden sie Beispielcode, wie ich post-Daten sende php curl

<?php 

     $doc_file_path_string = ' -F document[][email protected]_name'; 
     array_push($doc_data_array, $doc_file_path_string);  


     $url = 'https://server_url.com'; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, implode(' ',$doc_data_array)); 
     $headers = array(); 
     $headers[] = "Content-Type: multipart/form-data"; 
     $headers[] = "Accept: application/json"; 
     $headers[] = "Api-Token: api_key"; 
     $headers[] = "Api-Secret: api_key "; 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     echo $result = curl_exec($ch); 
    ?> 

Antwort

0

Hier verwenden ist, wie ich es tat

$url = <some url>; 
$ch = curl_init(); 
$headers = ["Content-Type:multipart/form-data"]; 

// Define curl options 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, true); 

// Here we make base64 encode for file content and push it into curl   
$base64 = base64_encode(file_get_contents(<path to file>)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $base64]); 

// Make request 
$response = curl_exec($ch); 
curl_close($ch); 
+0

Hallo, hat meine Antwort Ihnen geholfen? – Kison

Verwandte Themen