Search for:

PHP + CURL: Get Returned Content Mime Type

Did you ever tried to get mime type of the returned content in CURL! I was writing a small application for something and thought to know the Content-Type (mime type) of the response data returned by CURL's exec(). Before thinking of anything else, I habitually started Goggling and found this article after few minutes. It is a completely big page about getting content type of web page grabbed in various ways. Function curl_getinfo() return lots of useful information about CURL transfer.

Just have a looks here to know better about the function: curl_getinfo().

The function returns lots of other very useful information than only return headers. You may like to play with it.

curl_getinfo(curl resource) return all the mentioned data above. For specific info only, provide second parameters as CURLINFO_CONTENT_TYPE. Add the CURLINFO_ to above list and you will get needed data about that parameter.

In the case of CSS and JavaScript content type, the content type returned is directly a mime type (text/css, application/JavaScript). However, in case of HTML page, I see that it is returning content type as "text/html; charset=utf-8" . Why is this so? Need to investigate. Therefore, the mentioned page has used regex for extracting mime type of content. Here is the code:

I have added '-' to the their regex, so that it can return mime type like application/x-javascript also. Many Linux server can have JavaScript mime type set as application/x-javascript instead of just application/javascript.