cURL在存在的文件(远程服务器)上返回404。为什么? [英] cURL is returning 404 on a file that exists (remote server). Why?

查看:526
本文介绍了cURL在存在的文件(远程服务器)上返回404。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查是否存在某些mp3。尽管有时没问题,但某些有效的mp3文件显示为404(未找到)。这是我正在使用的代码:

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

我认为问题是因为网站正在使用重定向,但是将 CURLOPT_FOLLOWLOCATION设置为TRUE却没有

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

http代码在以下mp3上显示404。您可以在浏览器中转到它们,然后查看它们是否起作用。这些只是许多出现此问题的随机例子:

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

推荐答案

基本上:

@curl_exec($ch);

您假设curl执行程序正确,并丢弃其返回值。如果执行查询时出现问题,则exec返回false,curl_error和curl_errno将包含有关爆炸的内容和方式的诊断信息。永远不要假设卷曲成功。失败的原因太多,但只有一种成功的方法。至少,将代码更改为:

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.

这篇关于cURL在存在的文件(远程服务器)上返回404。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆