【问题标题】:How to send multiple files under the same name using CURL in php7+如何在 php7+ 中使用 CURL 发送多个同名文件
【发布时间】:2020-02-20 12:39:33
【问题描述】:

您好我正在尝试在 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;

【问题讨论】:

标签: laravel curl php-curl php-7.2


【解决方案1】:

我相信您只需要创建一个关联数组即可使用相同的键发送多个图像。

$post["image"] = [
    new \CurlFile('image_full_path1.png', 'image/png', 'file1.png'),
    new \CurlFile('image_full_path2.png', 'image/png', 'file2.png'),
];

【讨论】:

  • 已经尝试过这种方法。它给出了一个错误数组到字符串转换如果我再次对其进行 Json 编码它不起作用
  • if I Json encode it again 是什么意思?您可以发布该代码吗?我在您的解释中看不到该代码。然后您可以发布错误详细信息吗?
【解决方案2】:

问题似乎与我的请求目的地有关。 curl 的终点是写在 Python Flask 上的,看起来如果我们像在 php 中那样发送它,flask 不理解它。

所以在php中

image[0] => 'image1.png'
image[1] => 'image2.png'

可以有效,我们将在请求中获取image key下的两个文件,

在 python 中,它们有不同的键,例如

图像[0] 和图像[1]。

如果我们试图在请求中查找图像,它会报错,但图像[0]和图像[1]作为字符串可以正常工作。

解决方案是在关联数组中将文件作为 base 64 编码的 url 发送。

附:我不是 Python 专家,所以我的解释可能是错误的。如果有人知道 python 并且知道此问题,请在此处发表评论。

谢谢。

【讨论】:

  • "解决方案是在关联数组中将文件作为 base 64 编码的 url 发送"如何?
猜你喜欢
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多