在单个 CURL 请求中写入多个文件

2022-01-05 00:00:00 file-upload request curl php

有没有办法使用 PHP curl 在单个请求中发送多个文件?

Is there a way using PHP curl to send multiple files in a single request?

我知道您可以使用以下方式发送单个文件:

I understand you can send a single file making use of the following:

$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 个文件.

But I want to be able to write lets say 3 files using a single request.

有没有办法在 curl_exec() 之前将字节写入请求?

Is there maybe a way to write bytes to the request before the curl_exec()?

推荐答案

一个完整的例子应该是这样的:

A complete example would look something like this :

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


?>

相关文章