how to get response status in api using curl php?

To get the response status code in PHP using cURL, you can use the curl_getinfo() function after executing the cURL request. Here's an example:

<?
// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/endpoint");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL request
$response = curl_exec($ch);

// Get the HTTP response status code
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Close cURL session
curl_close($ch);

// Print the HTTP response status code
echo "Response status code: " . $http_status;
?>

 

Post your Answer