CURLOPT_NOBODY option returns unexpected HTTP code

2019-06-14 13:05发布

I have a server, called install64-7 in this example which I will access to check for the existance of a zip file, which is not on server. The following PHP code returns the HTTP returncode 200 even if the zip file does not exist on the server 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

In case I remove the option CURLOPT_NOBODY, the request gives a 404! see screenshot for second request

$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

How is this possible, what am I missing? What is this sorcery about the option CURLOPT_NOBODY? Thank you for any help

1条回答
Fickle 薄情
2楼-- · 2019-06-14 13:38

CURLOPT_NOBODY set to TRUE makes a HTTP HEAD request, as compared to the "normal" HTTP GET.

If you get a different repsonse code because of that it is simply because the server decides to respond differently - although it shouldn't according to the HTTP spec.

查看更多
登录 后发表回答