2016-10-27 3 views
5

Ich versuche, mehr als ein Bild mit PHP Curl zu laden. Die API Ich verwende gibt mir das folgende Beispiel:Laden Sie mehrere Bilder mit PHP Curl

curl -v -s -u username:password \ 
-H "Content-Type: multipart/form-data" \ 
-H "Accept: application/vnd.com.example.api+json" \ 
-F "[email protected];type=image/jpeg" \ 
-F "[email protected];type=image/jpeg" \ 
-XPUT 'https://example.com/api/sellers/12/ads/217221/images' 

Also in meinem PHP-Skript habe ich versucht, dies:

$files = array(); 

foreach($photos as $photo) { 
    $cfile = new CURLFile('../' . $photo, 'image/jpeg', 'test_name'); 
    $files[] = $cfile; 
} 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images'); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $files); 
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080'); 
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Host: services.example.com', 
    'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx', 
    'Accept: application/vnd.com.example.api+json' 
)); 

$output = curl_exec($ch); 

curl_close($ch); 

Zuerst habe ich diese Antwort von der API bekam:

{ 
    "errors":{ 
     "error":{ 
     "@key":"unsupported-form-element" 
     } 
    } 
} 

aber jetzt gibt es überhaupt keine Antwort. Wie verwende ich curl um mehrere Dateien hochzuladen?

Wenn $ files zum Beispiel ein leerer JSON ist, gibt es überhaupt keinen Fehler und gibt die Bilder zurück (natürlich leer).

Dies ist die Dokumentation der API Ich verwende: https://services.mobile.de/manual/new-seller-api.html#_upload_images

EDIT: Ich habe versucht, die Anfrage Körper und senden es zu bauen, aber es tut nichts:

$requestBody = ''; 
$requestBody .= '--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx\r\n'; 
$requestBody .= 'Content-Disposition: form-data; name="image"; filename="ferrari.JPG"\r\n'; 
$requestBody .= 'Content-Type: image/jpeg\r\n'; 
$requestBody .= 'Content-Transfer-Encoding: binary\r\n'; 

curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); 

Antwort

1

versuchen assoc Array, wie es in der Dokumentation verwenden verwenden CURLFile

$photos = [ 
    'img1' => 'img1.jpg', 
    'img2' => 'img2.jpg' 
] 

$files = []; 

foreach($photos as $key => $photo) { 
    $cfile = new CURLFile('../' . $photo, 'image/jpeg', $key); 
    $files[$key] = $cfile; 
} 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images'); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $files); 
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080'); 
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Host: services.example.com', 
    'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx', 
    'Accept: application/vnd.com.example.api+json' 
)); 

$output = curl_exec($ch); 

curl_close($ch); 
Verwandte Themen