You can use the curl_getinfo()
function in PHP to retrieve the HTTP response status code from a cURL request. Here is an example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP Response Status Code: " . $status_code;
In this example, we set the CURLOPT_RETURNTRANSFER
option to true
to store the response in the $response
variable. Then, we use the curl_getinfo()
function with the $ch
cURL handle and the CURLINFO_HTTP_CODE
constant to retrieve the HTTP response status code. Finally, we close the cURL session and echo the status code.