Getting Http Code in PHP Using Curl

Getting HTTP code in PHP using curl

First make sure if the URL is actually valid (a string, not empty, good syntax), this is quick to check server side. For example, doing this first could save a lot of time:

if(!$url || !is_string($url) || ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url)){
return false;
}

Make sure you only fetch the headers, not the body content:

@curl_setopt($ch, CURLOPT_HEADER  , true);  // we want headers
@curl_setopt($ch, CURLOPT_NOBODY , true); // we don't need body

For more details on getting the URL status http code I refer to another post I made (it also helps with following redirects):

  • How can I check if a URL exists via PHP?

As a whole:

$url = 'http://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo 'HTTP code: ' . $httpcode;

get ONLY the http status code with curl and php

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,"";
?>

Retrieve the response code from header cURL php

I think you need to pass $curl to the curl_getinfo method, not the $response

$response = curl_exec($curl);
$theInfo = curl_getinfo($curl);
$http_code = $theInfo['http_code'];

You can see the doco here.. https://www.php.net/manual/en/function.curl-getinfo.php

Get the right http status code php

Ahh! Google are locating you by GEO-IP or similar and redirecting you to your local google mirror.

See: https://webapps.stackexchange.com/questions/46591/what-does-gws-rd-cr-in-the-url-indicate

So as they're redirecting you the 302 code is correct.

Try using the URL: https://www.google.com/ncr
(ncr standing for No Country Redirect) and see how you go.

HTTP request with PHP CURL and getting different result from postman and curl cli

It turns out that this API requires the User-Agent header and it seems that Postman and the curl cli send those automatically while the php-curl request doesn't. As soon as I added that header and made up a value it started working.

PHP, CURL: function get the response code and return in variable

function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: text/plain');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);

if (empty($data) OR (curl_getinfo($ch, CURLINFO_HTTP_CODE == 404))) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch);
$data = null;
return $data;
} else {
// everything is ok
return $data;

}

}


Related Topics



Leave a reply



Submit