我正在使用 guzzle 从 url 中检索图像。其中一些网址来自 s3。s3 使用application/octet-stream
图像的内容类型。我认为我无法访问回调中的$request
对象,fulfilled
所以我看不到扩展名是否在 url 中。fulfilled
当内容类型为 时,我有什么方法可以猜测回调中的扩展名application/octet-stream
吗?
<?php
function getExtension(Response $response) {
$contentType = explode(';', $response->getHeaderLine('Content-Type'), 2)[0];
$extensions = [
'image/png' => '.png',
'image/jpg' => '.jpg',
'image/jpeg' => '.jpeg',
'image/gif' => '.gif',
'image/tiff' => '.tiff',
'image/bmp' => '.bmp',
'application/octet-stream' => false
];
return $extensions[$contentType] ?? null;
}
$client = new Client();
$pool = new Pool($client, $requests, [
'concurrency' => 5,
'fulfilled' => function ($response) use ($zip) {
$ext = getExtension($response);
if (null === $ext) {
return;
}
if (false === $ext) {
// figure out the extension somehow
// $ext = ".foobar";
}
$id = Uuid::uuid4()->toString();
$filename = $id . $ext;
$zip->addFromString($filename, (string)$response->getBody());
},
'rejected' => function ($reason, $index) {
// no-op
}
]
);
$pool->promise()->wait();