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

发布于 2024-10-30 22:25:00 字数 617 浏览 1 评论 0原文

有没有办法使用 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() 之前将字节写入请求?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生一个溪 2024-11-06 22:25:00

一个完整的例子看起来像这样:

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


?>

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


?>
花落人断肠 2024-11-06 22:25:00

你可以用这个

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

You can use this

$post = array(
     "file1"=>"@/path/to/myfile1.jpg",
     "file2"=>"@/path/to/myfile2.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
会发光的星星闪亮亮i 2024-11-06 22:25:00

我搜索了一种使用curl 发送包含多个文件的post 请求的方法。
但所有代码示例并没有像浏览器那样执行相同的操作。
$_FILES 全局不包含 PHP 记录的常用数组。

现在我找到了原因:缺少 [i] 的偏移量

请参见 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

I searched a way to send a post request with multiple files using curl.
But all code examples did not do the same as a browser does.
The $_FILES global did not contain the usual array as documented by PHP.

Now i found out why: the offsets where missing the [i]

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

Without the "trick" you would get a different result in $_FILES

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

or

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

But this is not what we want.

What we want is the usual $_FILES array like

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

Here the code:

Example:

$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文