CURLOPT_NOBODY选项在不存在的文件上返回意外的HTTP代码200

时间:2014-04-23 15:25:56

标签: php apache http curl

我在这个例子中有一个名为install64-7的服务器,我将访问该服务器以检查是否存在服务器上的zip文件。即使服务器200上不存在zip文件,以下PHP代码也会返回HTTP返回码install64-7

$srcPath = "http://install64-7/TestApp.zip";
$ch = curl_init( $srcPath );
curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_exec( $ch );
$retcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
unset( $ch );
var_dump($retcode);
exit;

enter image description here

如果我删除选项CURLOPT_NOBODY,请求会提供404!查看第二次请求的屏幕截图

$srcPath = "http://install64-7/TestApp.zip";
$ch = curl_init( $srcPath );
//curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_exec( $ch );
$retcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
unset( $ch );
var_dump($retcode);
exit;

enter image description here

这怎么可能,我错过了什么?关于选项CURLOPT_NOBODY的这种魔法是什么? 感谢您的帮助

1 个答案:

答案 0 :(得分:3)

CURLOPT_NOBODY设置为TRUE会产生HTTP HEAD请求,与" normal"相比。 HTTP GET。

如果你得到一个不同的repsonse代码,因为它只是因为服务器决定以不同的方式响应 - 尽管它不应该根据HTTP规范。