Upload multiple images with PHP Curl

后端 未结 1 994
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 17:37

I\'m trying to upload more than one image with PHP Curl. The API I\'m using gives me the following example:

curl -v -s -u username:password \\
 -H \"Content-         


        
相关标签:
1条回答
  • 2020-12-11 18:19

    try use assoc array, how it use into documentation 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);
    
    0 讨论(0)
提交回复
热议问题