开发者

cURL is returning 404 on a file that exists (remote server). Why?

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 j开发者_如何学运维ust 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜