我需要检查URL是否使用PHP返回PDF文档。现在我正在使用file_get_mimetype函数。一个普通的URL(https://www.google.com/)返回类型为application/octet-stream,而一个普通的pdf链接(http://www.brainlens.org/content/newsletters/Spring%202013.pdf)返回类型为application/pdf。但现在我也遇到URL,如http://www.dadsgarage.com/~/media/Files/example.ashx或http://www.wpdn.org/webfm_send/241,这也是pdf,但返回应用程序/八字节流。也有其他URL打开另存为对话框,也必须被检测到
使用get_headers()
,
$url = "http://www.dadsgarage.com/~/media/Files/example.ashx";
if (in_array("Content-Type: application/pdf", get_headers($url))) {
echo "URL returns PDF";
}
print_r(get_headers($url));
返回Array
(
[0] => HTTP/1.1 200 OK
[1] => Cache-Control: private, max-age=604800
[2] => Content-Length: 194007
[3] => Content-Type: application/pdf
[4] => Expires: Tue, 09 Dec 2014 09:40:20 GMT
[5] => Last-Modified: Wed, 07 Aug 2013 16:46:30 GMT
[6] => Accept-Ranges: bytes
[7] => Server: Microsoft-IIS/8.0
[8] => Content-Disposition: inline; filename="example.pdf"
[9] => X-AspNet-Version: 4.0.30319
[10] => X-Powered-By: ASP.NET
[11] => X-Provider: AWS
[12] => Date: Tue, 02 Dec 2014 09:40:20 GMT
[13] => Connection: close
)
mime类型可以包括:
application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf