I'm trying to get only the three digit http status code number and nothing more in the variable $response. For example 302, 404, 301, and what not. Another observation I noticed with my code is on some websites such as google it's downloading what appears to be part of the body, which is a huge waste of bandwidth and slow.
<?php
$URL = 'http://www.google.com';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
$response = curl_exec($curlHandle);
echo $response;
?>
You can set the CURLOPT_NOBODY
option to not receive a body. Then you can get the status code with curl_getinfo
.
Like so:
<?php
$URL = 'http://www.google.com';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY , true); // we don't need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don't forget to close the connection
echo $response,"";
?>
First off, you get only the headers (CURLOPT_NOBODY).
Then you capture the HTML as the result (CURLOPT_RETURNTRANSFER).
Finally you extract the HTTP code with a regex that gets the first numbers surrounded by spaces.
$URL = 'http://www.google.com';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_NOBODY, true);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curlHandle);
preg_match('/ \d+ /', $response, $matches);
$response = $matches[0];
For just the status code you can use this:
function getStatusCode($url) {
$headers = get_headers($url);
preg_match('/\s(\d+)\s/', $headers[0], $matches);
return $matches[0];
}
echo getStatusCode('http://www.google.com');
curl_exec($curlHandle);
$curl_info = curl_getinfo($curlHandle);
$response = $curl_info['http_code'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.