0

I've got a $response variable, which implements Psr\Http\Message\ResponseInterface.

It contains info about downloaded file. How can I get mime-type of this file?

userlond
  • 3,632
  • 2
  • 36
  • 53
  • 1
    You could try `$response->getHeader('content-type')`. See https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php#L94 – Phil Apr 18 '17 at 03:57
  • @Phil, for me `$response->getHeader('Content-Type')[0]` works perfectly. You may answer and I'll accept the answer. – userlond Apr 18 '17 at 04:46

1 Answers1

4

Since ResponseInterface extends MessageInterface, you can use the getHeader($name) or getHeaderLine($name) method to retrieve the value of the Content-Type response header. For example...

$mimeType = $response->getHeaderLine('Content-Type');

Note: I used getHeaderLine as it's unlikely the Content-Type header will contain more than one value and this will save you treating the return value as a single-item array.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • You say unlikely, while I consistently get content types such as `application/json;charset=UTF-8`. The `getHeader()` method in the Guzzle implementation does not split that up into two parts, as I would expect. Should PSR-7 be able to handle that extra "charset" part, or is that up to the application to do? – Jason Jul 04 '18 at 14:01
  • @Jason that's still only one header. The thing with HTTP headers is that they may be repeated with different values so robust implementations will always offer a collection of values per header name – Phil Sep 03 '19 at 06:27