PHP code to check if remote mp3 exists

Hi, I’ve a big table with thousands of mp3 links. Sice these links come from an old database, many of…

November 1, 2011

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;
	}
}

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

Scraping content with PHP as if it was jQuery

Building a spider or a bot needs some knowledge of regular expressions, you must know and use preg_match or preg_match_all…

December 8, 2013

Get instagram data without official api in PHP

Instagram has an official API to interact with its database of images and users. If you have enough time to…

December 3, 2013

Make a cron job with IFTTT

Cron is a software utility, a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain…

November 12, 2013

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

October 8, 2012

How to use Instagr.am photos on your site

UPDATE: 2013-12-04 I’ve made a method in the Mini Bots PHP Class that lets you retrieve images from instagram without…

August 18, 2011

get MySpace events with a PHP function

Here is a function to read the concerts for a myspace band page. This code retrieves the “shows page” for…

February 21, 2011