Php 从异步guzzle请求获取文件扩展名
我正在使用guzzle从URL检索图像。其中一些URL来自s3。s3对图像使用Php 从异步guzzle请求获取文件扩展名,php,guzzle,guzzle6,Php,Guzzle,Guzzle6,我正在使用guzzle从URL检索图像。其中一些URL来自s3。s3对图像使用应用程序/octet流内容类型。我认为我无权访问已完成的回调中的$request对象,因此无法查看扩展名是否在url中。当内容类型为application/octet-stream时,我是否有办法猜测completed回调中的扩展名 <?php function getExtension(Response $response) { $contentType = explode(';', $respons
应用程序/octet流
内容类型。我认为我无权访问已完成的
回调中的$request
对象,因此无法查看扩展名是否在url中。当内容类型为application/octet-stream
时,我是否有办法猜测completed
回调中的扩展名
<?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();
不幸的是,对于Guzzle 6,我们失去了执行以下操作的能力:$response->getEffectiveUrl()
您所需要的只是将请求url写入响应头的中间件(或处理程序)。然后可以通过$response->getHeader('myRequestUrlHeader')[0]
访问url