0

I'm checking to see if certain mp3's exist. While sometimes there's no problem, certain valid mp3 files are shown as 404, not found. Here's the code I'm using:

$ch = @curl_init($file_path);
@curl_setopt($ch, CURLOPT_HEADER, true);
@curl_setopt($ch, CURLOPT_NOBODY, true);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

@curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
echo "<pre>";
print_r($header);
echo "<pre>";

I thought that the problem was because the sites were using redirects, but setting "CURLOPT_FOLLOWLOCATION" to TRUE didn't solve the problem.

The http code shows 404 on the following mp3s. You can go to them in your browser and see that they work. These are just a few random examples of many that have this problem:

http://audio.arjlover.net/audio/pesni/Babies_go_series/Babies%20Go%20Beatles/01%20Hey%20Jude%20Beatles.mp3

http://www.dagatinha.com.br/musicas/Lady%20Gaga%20%20-%20Just%20Dance.mp3

hakre
  • 193,403
  • 52
  • 435
  • 836
timetofly
  • 2,957
  • 6
  • 36
  • 76
  • 2
    On an unrelated note, do you happen to own the copyright on those music files? – DevSolar Jul 19 '10 at 17:37
  • 1
    @DevSolar: Are you asking if user371699 is actually Lady Gaga? – webbiedave Jul 19 '10 at 17:43
  • 2
    Lady Gaga would use Ruby – stimms Jul 19 '10 at 17:45
  • 1
    I didn't check, but some server-side script maybe returns a 404 if it doesn't recognize your user-argent – Tomaka17 Jul 19 '10 at 17:46
  • What does `$file_path` contain? – Matthew Jul 19 '10 at 17:46
  • @stimms: I'm pretty sure she uses Phparazzi. – webbiedave Jul 19 '10 at 18:00
  • DevSolar: I don't, but make sure to use files legally. In any case, I try and filter them. I just posted two random ones with the problem. Tomaka1: So you think there's no way around this? konforce: $file_path simply contains the URL to the mp3. Bad variable name. Thanks. – timetofly Jul 19 '10 at 18:13
  • @user371699 : with the current code, they're both OK 200's to me. Possibly a loadbalancing glitch (you may not end up on the server I end up on) or you have hammered to much on the server resulting in some exotic headers just to prevent you from hammering? – Wrikken Jul 19 '10 at 18:18
  • @user371699, I asked about the contents of `$file_path` because the code works perfectly fine for me as-is with the two URLs you supplied as `$file_path` (i.e., using the %20 encoding). So if your `$file_path` is correct, I'd check the sort of things that Wrikken is talking about. – Matthew Jul 19 '10 at 18:55
  • Thanks for looking into that for me. Looks like there is some sort of problem on my end--I'll look into it. Thanks for the help! – timetofly Jul 19 '10 at 19:00
  • @webbiedave: Or authorized to advertize her songs for free downloads. ;-) – DevSolar Jul 20 '10 at 06:43

1 Answers1

1

Basically:

@curl_exec($ch);

You're assuming curl exec'd properly, and throw away its return value. If there was a problem doing the query, exec returns false and curl_error and curl_errno will contain diagnostic information as to what blew up and how. Never assume curl succeeded. Far too many reasons for it to fail, but only one way to succeed. At minimum, change your code to:

if (curl_exec($ch) === FALSE) {
    die("Curl error: " . curl_error($ch));
}

to see why things died. Otherwise you're just left with odd results and have thrown away any chance of seeing why they occured.

Marc B
  • 356,200
  • 43
  • 426
  • 500