【问题标题】:Can't get Guzzle to force content-type for raw image data无法让 Guzzle 强制原始图像数据的内容类型
【发布时间】:2016-12-16 23:51:43
【问题描述】:

我正在使用 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=... />" 标记中。

【问题讨论】:

    标签: php curl guzzle


    【解决方案1】:

    您不能强制响应的内容类型。

    您使用的 Content-Type 标头是 request 的内容类型,而不是 response

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

    【讨论】:

    • 谢谢...我终于意识到我可以对返回的流中的数据做任何我想做的事情;在这种情况下,如果我将其编码为 base64 并在其前面放置正确的提示:data:image/jpeg;base64, -- html <img> 标签工作正常。
    猜你喜欢
    • 2015-03-29
    • 2018-12-26
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2010-10-03
    • 1970-01-01
    相关资源
    最近更新 更多