如何在 php7+ 中使用 CURL 发送同名的多个文件
How to send multiple files under the same name using CURL in php7+
您好,我正在尝试在 php Curl 操作中发送多个文件。我正在使用 PHP7.2 并尝试在同一个密钥下发送 15 张图像。
如果我尝试只发送 1 个文件,它工作正常。
$post["image"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
但是当我尝试放置多个文件时它不再工作了。
已经试过了
$post["image[0]"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
和
$post["image_0"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
无效
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$path = '/var/www/download/download.pdf';
file_put_contents($path, $response);
echo $response;
我相信你只需要创建一个关联数组来使用相同的键发送多个图像。
$post["image"] = [
new \CurlFile('image_full_path1.png', 'image/png', 'file1.png'),
new \CurlFile('image_full_path2.png', 'image/png', 'file2.png'),
];
问题似乎出在我的请求目的地。 curl 的终点写在 Python Flask 上,看起来如果我们像在 php 中那样发送它,flask 不理解它。
所以在php
image[0] => 'image1.png'
image[1] => 'image2.png'
可以有效,我们会得到请求中image key下的两个文件,
在 python 中,它们以不同的键出现,例如
图片[0] 和图片[1].
如果我们试图在请求中寻找图像,它会给出一个错误,但图像[0] 和图像[1] 作为一个字符串工作正常。
解决方案是将文件作为 base 64 编码 url 发送到关联数组中。
P.S。我不是 Python 方面的专家,所以我的解释可能是错误的。如果有人知道 python 并且知道这个问题,请在此处发表评论。
谢谢。
您好,我正在尝试在 php Curl 操作中发送多个文件。我正在使用 PHP7.2 并尝试在同一个密钥下发送 15 张图像。 如果我尝试只发送 1 个文件,它工作正常。
$post["image"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
但是当我尝试放置多个文件时它不再工作了。
已经试过了 $post["image[0]"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
和
$post["image_0"] = new \CurlFile('image_full_path.png', 'image/png', 'file.png');
无效
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$path = '/var/www/download/download.pdf';
file_put_contents($path, $response);
echo $response;
我相信你只需要创建一个关联数组来使用相同的键发送多个图像。
$post["image"] = [
new \CurlFile('image_full_path1.png', 'image/png', 'file1.png'),
new \CurlFile('image_full_path2.png', 'image/png', 'file2.png'),
];
问题似乎出在我的请求目的地。 curl 的终点写在 Python Flask 上,看起来如果我们像在 php 中那样发送它,flask 不理解它。
所以在php
image[0] => 'image1.png'
image[1] => 'image2.png'
可以有效,我们会得到请求中image key下的两个文件,
在 python 中,它们以不同的键出现,例如
图片[0] 和图片[1].
如果我们试图在请求中寻找图像,它会给出一个错误,但图像[0] 和图像[1] 作为一个字符串工作正常。
解决方案是将文件作为 base 64 编码 url 发送到关联数组中。
P.S。我不是 Python 方面的专家,所以我的解释可能是错误的。如果有人知道 python 并且知道这个问题,请在此处发表评论。
谢谢。