无法让 Guzzle 强制原始图像数据的内容类型

Can't get Guzzle to force content-type for raw image data

我正在使用 Guzzle 6,但从 REST 端点取回图像数据时遇到问题。

$client = new Client(['base_uri' => $base_uri]);
$type = 'POST';  //..or 'GET'...doesn't make a difference
$url = //..the endpoint where the image is served from
$headers['Content-Type'] = 'text/xml; charset=x-user-defined';
$response = $client->request($type, $url, ['headers'=>$headers, 'decode_content' => false] );

我强制使用 MIME 类型的请求被忽略了,返回的内容类型始终是 "image/jpeg",并且数据被篡改了,所以我不能简单地将它粘贴到 html "<img src=... />"标签。

您不能强制响应的content-type。

您使用的 Content-Type header 是 请求 的 content-type 而不是 响应 .

如果您使用的端点 (API?) 使您能够控制响应,它可能会基于您正在使用的 URL,但这不是您想要的可以告诉 Guzzle 改变。