Php 在一个CURL请求中写入多个文件

Php 在一个CURL请求中写入多个文件,php,file-upload,curl,request,Php,File Upload,Curl,Request,有没有一种方法可以使用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,

有没有一种方法可以使用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\u exec()
之前,是否有可能将字节写入请求?

您可以使用此选项

$post = array(
     "file1"=>"@/path/to/myfile1.jpg",
     "file2"=>"@/path/to/myfile2.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
你可以用这个

$post = array(
     "file1"=>"@/path/to/myfile1.jpg",
     "file2"=>"@/path/to/myfile2.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

完整的示例如下所示:

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


?>
“@/Users/whoho/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);
卷曲设置($ch,卷曲设置桩,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);
回波旋度误差($ch);
卷曲关闭($ch);
?>

完整的示例如下所示:

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


?>
“@/Users/whoho/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);
卷曲设置($ch,卷曲设置桩,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);
回波旋度误差($ch);
卷曲关闭($ch);
?>

我使用curl搜索了一种发送包含多个文件的post请求的方法。 但是,所有的代码示例并不像浏览器那样做。 $\u文件global不包含PHP记录的常用数组

现在我发现了原因:缺少[i]

如果没有这个“技巧”,您将在$\u文件中得到不同的结果

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

但这不是我们想要的

我们想要的是通常的$\u文件

[
    '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

我使用curl搜索了一种发送包含多个文件的post请求的方法。 但是,所有的代码示例并不像浏览器那样做。 $\u文件global不包含PHP记录的常用数组

现在我发现了原因:缺少[i]

如果没有这个“技巧”,您将在$\u文件中得到不同的结果

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

但这不是我们想要的

我们想要的是通常的$\u文件

[
    '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

如果您使用的是PHP5.5+,这将不起作用。您需要这样做:如果您使用的是PHP5.5+,这将不起作用。您需要这样做: