1

I want to check a MP3 link if it exists or corrupted.

I have a link http://a.tumblr.com/tumblr_l1k295O4mS1qa64mao1.mp3 it is working. But http://a.tumblr.com/tumblr_l1k295O4mS1qa64maosadasdasdasdasd1.mp3 is a corrupted MP3 file link.

I want idea or code where I can check a mp3 link if it is working or corrupted using PHP.

Updated :

I think it is right. i have found on Check if links are broken in php

function check_url($url) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);

return $headers['http_code'];
}

$check_url_status = check_url($url);
if ($check_url_status == '200')
 echo "Link Works";
else
  echo "Broken Link";
Community
  • 1
  • 1
Rinku Yadav
  • 61
  • 1
  • 11
  • What exactly are your requirements? Is it enough that the URL doesn't give a `404 Not Found` error? Does it have to point to a valid MP3 music file? Is it ok if it leads to a valid MP3 file after redirects, or does it have to be direct? – Nico Mar 09 '14 at 15:01
  • the first link seems to be responding with mp3 data and HTTP 200 OK, whereas the latter responds with HTTP 403 forbidden. Just check the HTTP response status code? – eis Mar 09 '14 at 15:01
  • But using php how to check it is returning 404 or 200 responds ? – Rinku Yadav Mar 09 '14 at 15:08
  • As @Nico says, do you wish to check the validity of the MP3 file? – halfer Mar 09 '14 at 16:12

1 Answers1

1

The second link results in a 403 response from the server. The simplest solution would be to use get_headers():

$url = '...';
$headers = get_headers($url);

// warning: this will also accept 
if (strpos($headers[0], '403 Forbidden')) {
  // link invalid
}

Another way is to check for the AccessDenied value in the actual XML output:

<Error>
  <Code>AccessDenied</Code>
  <Message>Access Denied</Message>
  <RequestId>...</RequestId>
  <HostId>
    ...
  </HostId>
</Error>
ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • 1
    probably anything else than 200 is "invalid" – eis Mar 09 '14 at 15:03
  • But using php how to check it is returning 404 or 200 responds ? – Rinku Yadav Mar 09 '14 at 15:09
  • @RinkuYadav Read the documentation about [get_headers()](http://php.net/get_headers). There are some examples which show that the first entry of the returned array will contain the response code. – ComFreek Mar 09 '14 at 15:10