Запись нескольких файлов в одном запросе CURL


Есть ли способ с помощью PHP curl отправить несколько файлов в одном запросе?

Я понимаю, что вы можете отправить один файл, используя следующее:

$fh = fopen("files/" . $title . "/" . $name, "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);

Но я хочу иметь возможность писать, скажем, 3 файла, используя один запрос.

Может быть, есть способ записать байты в запрос до curl_exec()?

Author: Koekiebox, 2011-04-06

3 answers

Полный пример будет выглядеть примерно так:

<?php
$xml = "some random data";
$post = array(
     "uploadData"=>"@/Users/whowho/test.txt", 
     "randomData"=>$xml, 
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, trim("http://someURL/someTHing"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);


?>
 8
Author: ChristiaanP, 2011-12-09 14:19:01

Вы можете использовать это

$post = array(
     "file1"=>"@/path/to/myfile1.jpg",
     "file2"=>"@/path/to/myfile2.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
 5
Author: strauberry, 2011-04-06 15:24:59

Я искал способ отправить запрос post с несколькими файлами с помощью curl. Но все примеры кода делали не то же самое, что браузер. Глобальный файл $_FILES не содержал обычного массива, задокументированного PHP.

Теперь я выяснил, почему: смещения, в которых отсутствует [я]

См. http://php.net/manual/de/class.curlfile.php#121971

Без "трюка" вы получили бы другой результат в $_FILES

[
    'foo_bar' => [
        'name'     => 'path/to/my_file_1.png',
        'type'     => 'application/octet-stream',
        'tmp_name' => '/tmp/phpim24ij',
        'error'    => 0,
        'size'     => 123,
    ],
]

Или

[
    'foo_bar_1' => [
        'name'     => 'path/to/my_file_1.png',
        'type'     => 'application/octet-stream',
        'tmp_name' => '/tmp/phpim24ij',
        'error'    => 0,
        'size'     => 123,
    ],
    'foo_bar_2' => [
        'name'     => 'path/to/my_file_1.png',
        'type'     => 'application/octet-stream',
        'tmp_name' => '/tmp/phpim24ij',
        'error'    => 0,
        'size'     => 123,
    ],
]

Но это не то, чего мы хотим.

То, что мы хотим, - это обычный массив $_FILES, подобный

[
    'foo_bar' => [
        'name'     => [
            0 => 'path/to/my_file_1.png',
            1 => 'path/to/my_file_2.png',
        ],
        'type'     => [
            0 => 'application/octet-stream',
            1 => 'application/octet-stream',
        ],
        'tmp_name' => [
            0 => '/tmp/phpSPAjHW',
            1 => '/tmp/php0fOmK4',
        ],
        'error'    => [
            0 => 0,
            1 => 0,
        ],
        'size'     => [
            0 => 123,
            1 => 234,
        ],
    ],
]

Вот код:

Пример:

$url = "http://api-foo.com/bar/baz";
$uploadFormInputFieldName = 'foo_bar';
$files = [
    'path/to/my_file_1.png',
    'path/to/my_file_2.png',
    // ...
];

$postData = [];
$i = 0;
foreach ($files as $file) {
    if (function_exists('curl_file_create')) {
        $file = curl_file_create($file);
    } else {
        $file = '@' . realpath($file);
    }
    // here is the thing: post data needs to be $posData["foo_bar[0]"], $posData["foo_bar[1]"], ...
    $postData["{$uploadFormInputFieldName}[{$i}]"] = $file;
    $i++;
}

$httpClient = $this->getHttpClient($url); // get your client
$httpClient->setHeader('Content-Type', 'multipart/form-data');
$response = $httpClient->post($postData); // send post request
 1
Author: cottton, 2018-05-04 14:38:00