检查 URL 是否返回 PDF

Man*_*mar 0 php url file-get-contents

我需要检查 URL 是否使用 PHP 返回 PDF 文档。现在我正在使用 file_get_mimetype 函数。普通 URL ( https://www.google.com/ ) 返回类型为 application/octet-stream 而普通 pdf 链接 ( http://www.brainlens.org/content/newsletters/Spring%202013.pdf ) 返回应用程序/pdf。但现在我也遇到了像http://www.dadsgarage.com/~/media/Files/example.ashxhttp://www.wpdn.org/webfm_send/241这样的URL ,它也是 pdf 但返回 application/octet-溪流。还有其他 URL 也会打开一个另存为对话框,也必须检测到。

Soo*_*K G 7

使用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));
Run Code Online (Sandbox Code Playgroud)

返回

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
)
Run Code Online (Sandbox Code Playgroud)