I'm trying to test server response from a few client websites using cURL to retrieve only the header, wrapped in a microtime call to calculate full execution time (for server roundtrip), and the HTTP status code so that myself and the client can be aware of any issues.
I need to call cURL by server IP with the host defined there as I want to be 100% sure to eliminate DNS server downtime - I'm using another script to make sure my DNS copies are up to date, so that's not an issue.
I'm using the following code which is working on 90% of servers, but the odd few are rejecting with 400 and 404 codes despite being accessible in a browser.
// Setup headers
$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Host: $this->url";
$header[] = "Keep-Alive: 300";
$header[] = "Pragma: "; // browsers keep this blank.
$starttime = microtime(true);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://{$this->ip}/");
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_USERAGENT,"MyMonitor/UpCheck");
// curl_setopt($curl, CURLOPT_REFERER, 'http://www.mysite.com/');
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); //timeout in seconds
$this->header = curl_exec($curl);
$this->statuscode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
The code is wrapped within an Object and all relevant variables are correctly passed, trimmed, and sanitised. Because I need to call the server IP, this is passed as CURLOPT_URL, with the URL passed in the Header. I've tried setting the referer, but this didn't help.
Thanks,