$sniffer = new Smartycode_Http_Mime();
$contentType = $sniffer->getMime($url);
if('audio/mpeg3' == $contentType) echo 'It is MP3';
<?php
/*
* @author Danila Vershinin (smartycode.com)
*/
require_once 'Zend/Http/Client.php';
class Smartycode_Http_Mime {
private static $client = null;
public function getMime($url) {
if (!self::$client) {
self::$client = new Zend_Http_Client();
self::$client->setConfig(array(
'maxredirects' => 5,
'timeout' => 30,
'strictredirects' => true));
}
try {
self::$client->setUri($url);
} catch (Exception $e) {
Zend_Registry::get('logger')->debug($url. ' is not valid URL by W3C rules');
return null;
}
//$client = new Zend_Http_Client($url);
$mime = null;
try {
$response = self::$client->request('HEAD');
$content_type = $response->getHeader('Content-Type');
if (is_array($content_type) && count($content_type)) $content_type = $content_type[0];
list($mime) = explode(';', $content_type);
} catch (Exception $e) {
Zend_Registry::get('logger')->debug('Error while getting mime type for '.$url);
unset($response);
unset($content_type);
}
unset($client);
return $mime;
}
}
?>
< Prev | Next > |
---|