Site icon David Yin's Blog

Solve the Trouble with a cURL request in PHP

I have a HTTP status check online tool here. It is made by PHP with cURL extension.
The trouble is some responses are good. Someone tell me that the url should be 302 redir or 200 Ok, but the tool shows 403 Forbidden.
After doing some search and study, I find the problem of some server that don’t accept the Header request only.
Anytime you set the option…
PHP Code:

curl_setopt($ch, CURLOPT_NOBODY, true);

CURLOPT_NOBODY automatically shifts the request to a HEAD, type request, so most servers don’t allow HEAD type requests, results most times in 403 errors, so you use…
PHP Code:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘GET’);

So cURL, will send the request as GET or POST, but still only really make a HEAD request because the CURLOPT_NOBODY flag was set to true, which causes cURL to read only up to the end of the response header.


The solution found here.

Exit mobile version