php cURL Response Code

In PHP, you can use the curl_getinfo function to retrieve the HTTP response code from a cURL handle. This function returns an associative array of information about a cURL transfer, including the response code.

Here’s an example of how to use curl_getinfo to retrieve the response code:

<?php

// Set up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Send the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    die("cURL error: " . curl_error($ch));
}

// Close the cURL handle
curl_close($ch);

// Get the response code
$info = curl_getinfo($ch);
$http_code = $info['http_code'];

echo "HTTP response code: $http_code";

?>

This example sends a request to the https://www.example.com URL using cURL and then retrieves the response code from the returned information. The response code is stored in the $http_code variable and can be used for further processing or error handling.

Leave a Comment