try {
$url = 'http://nos.netease.com/edu-lesson-pdfsrc/1876F35C47CA72ABB8CEBBFCEC977149-1545094506108?Signature=16yL1%2Fh0syOXtPS9ydfWUFXF4PoZwE5vJTedcZivcDA%3D&Expires=1556782895&NOSAccessKeyId=7ba71f968e4340f1ab476ecb300190fa&download=%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%B5%B7%E6%BA%90.pdf';
$url = 'http://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/nos/mp4/2018/12/16/1010886261_c51c5d93ba0b4960b2ebc7a3be7a9cf9_hd.mp4';
echo get_remote_file_ext($url);
} catch(Exception $e) {
echo $e->getMessage();
}
function get_remote_file_ext($url) {
$ext = 'unknown';
stream_context_set_default(array('http'=>array('method'=>'GET', 'Host' => 'nos.netease.com')));
$headers = get_headers($url, 1);
if(isset($headers[0])) {
$splits = explode(' ', $headers[0]);
if(count($splits) < 2 ) {
throw new Exception($headers[0]);
return $ext;
}
if($splits[1] != '200') {
throw new Exception($splits[1]);
return $ext;
}
}
if(isset($headers['Content-Disposition'])) {
if(preg_match('/filename="[\S]*?\.([\w]*?)"/i', $headers['Content-Disposition'], $match)) {
$ext = $match[1];
}
} elseif (isset($headers['Content-Type'])) {
if(preg_match('/([\S]*?);/i', $headers['Content-Type'], $match)) {
$mime_type = $match[1];
if(($mime = get_mime($mime_type)) == -1) {
return $ext;
} else {
$ext = $mime;
}
}
}
return $ext;
}
function get_mime($mime_type) {
$match_mime = -1;
// mime列表:https://github.com/bcit-ci/CodeIgniter/blob/master/application/config/mimes.php
$mimes = array(
'hqx' => array('application/mac-binhex40', 'application/mac-binhex', 'application/x-binhex40', 'application/x-mac-binhex40'),
'cpt' => 'application/mac-compactpro',
'mp4' => 'video/mp4',
);
foreach($mimes as $mime => $mime_types) {
if(gettype($mime_types) == 'array') {
if(in_array($mime_type, $mime_types)) {
$match_mime = $mime;
break;
}
} elseif(gettype($mime_types) == 'string') {
if($mime_type == $mime_types) {
$match_mime = $mime;
break;
}
}
}
return $match_mime;
}