Nov 01 2011

PHP code to check if remote mp3 exists

Category: Php,Spiders & web botsGiulio Pons @ 4:30 pm

Hi, I’ve a big table with thousands of mp3 links. Sice these links come from an old database, many of them are old and expired. Here is a function that I’ve included in my Minibots Class. The function uses checkdnsrr to verify the domain and then uses curl to fetch the mp3 file and verify the mime type. I’ve used checkdnsrr first because it seems faster.

function checkMp3($url) {
	if (!function_exists("curl_init")) die("getHttpResponseCode needs CURL module, please install CURL on your php.");
	$a = parse_url($url);
	if(checkdnsrr(str_replace("www.","",$a['host']),"A") || checkdnsrr(str_replace("www.","",$a['host']))) {
		$ch = @curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_NOBODY, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		$results = explode("\n", trim(curl_exec($ch)));
		$mime = "";
		foreach($results as $line) {
			if (strtok($line, ':') == 'Content-Type') {
				$parts = explode(":", $line);
				$mime = trim($parts[1]);
			}
		}
		return $mime=="audio/mpeg";
	} else {
		return false;
	}
}
Share

Related posts:

  1. Test if a remote url exists with PHP and CURL
  2. PHP Web page to text function
  3. Copying remote files on your server with PHP
  4. get MySpace events with a PHP function
  5. PHP curl bot to update Facebook status

Leave a Reply