反正有拆分卷曲请求吗?

Is there anyway to split curl request?

我目前正在制作一个 PHP 文件来将多张图片上传到图片服务器上传。其中接受了 multipart/form-data。问题是他们在每个 CURL 请求中最多只接受 10 张图像。

------WebKitFormBoundaryXXXXXXXXXXXX
Content-Disposition: form-data; name="image"; filename="IMG_00910"
Content-Type: image/png    ------WebKitFormBoundaryXXXXXXXXXXXX
------WebKitFormBoundaryXXXXXXXXXXXX

Content-Disposition: form-data; name="image"; filename="IMG_00911"
Content-Type: image/png    ------WebKitFormBoundaryXXXXXXXXXXXX
------WebKitFormBoundaryXXXXXXXXXXXX

Content-Disposition: form-data; name="image"; filename="IMG_00XXX"
Content-Type: image/png    ------WebKitFormBoundaryXXXXXXXXXXXX

在示例中,我将 20 个请求上传为数组,如下所示:

$imgArr = array(
    'IMG_00900',
    'IMG_00901',
    'IMG_00902',
    'IMG_00903',
    'IMG_00904',
    'IMG_00905',
    'IMG_00906',
    'IMG_00907',
    'IMG_00XXX'
);
$startUpload = upload($imgArr);
$prin_r($ret);
//{"status":"true","data":["https://example.com/uploaded/IMG_00910","https://example.com/uploaded/IMG_00911","https://example.com/uploaded/IMG_00912","https://example.com/uploaded/IMG_00XX"]}

如果请求包含小于 10 个 IMG 请求,一切正常,所以如果我上传 100 个 IMG,我如何拆分 CURL 10 个请求?

将所有图像放在一个数组中(无论有多少):

$imgArr = array(
    'IMG_00900',
    'IMG_00901',
    'IMG_00902',
    'IMG_00903',
    'IMG_00904',
    'IMG_00905',
    'IMG_00906',
    'IMG_00907',
    'IMG_00XXX',
    ...
);

然后我们可以使用 array_chunk() 将数组分成最多 10 个元素的块。

现在我们只需要遍历块并一次发送一个块。

foreach (array_chunk($imgArr, 10) as $imgSet) {
    $response = upload($imgSet);
    // Do something with the response, if you need to
}