2016-10-27 5 views
5

PHP Curl로 둘 이상의 이미지를 업로드하려고합니다.PHP Curl로 여러 이미지 업로드

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

$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); 

우선은 API에서이 응답을 얻었다 :

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' 

그래서 내 PHP 스크립트에서 나는이 시도

: 내가 사용 API는 나에게 다음과 같은 예제를 제공합니다

하지만 지금은 전혀 응답이 없습니다. 컬을 사용하여 여러 파일을 업로드하려면 어떻게해야합니까?

$ files가 예를 들어 비어있는 JSON 인 경우 오류를 전혀주지 않고 이미지를 반환합니다 (물론 비어 있음).

은 내가 사용의 API 문서입니다 : https://services.mobile.de/manual/new-seller-api.html#_upload_images

편집 : 나는 그것을 요청 본문을 구축하고 보내려고하지만 아무것도하지 않습니다

$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); 

답변

1

는 문서에 사용하는 방법 ASSOC 배열를 사용하려고 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);