Current Location: Home> Latest Articles> curl_close How to get the requested HTTP status code after closing the session?

curl_close How to get the requested HTTP status code after closing the session?

gitbox 2025-05-20

When using PHP for HTTP requests, cURL is a very common tool that helps developers send requests and get responses. When using cURL to send a request and process a response, you may encounter a problem - how can you get the requested HTTP status code after calling curl_close() to close the cURL session?

In PHP, curl_close() is used to close a cURL session, which means that the function releases all resources related to the current session. Usually, after calling curl_close() , all session information will be destroyed, so the requested HTTP status code cannot be directly retrieved. However, we still have some ways to get the status code before or after closing the session.

1. Get HTTP status code through curl_getinfo()

After executing the request, you can use the curl_getinfo() function to obtain request information including the HTTP status code. This function can return an array containing multiple request information, one of which is http_code , which represents the HTTP status code.

Before closing the cURL session, we can call curl_getinfo() to get the status code. Here is an example:

 <?php
// initialization cURL Session
$ch = curl_init();

// Set requested URL
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/example");

// set up cURL Options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// implement cURL ask
$response = curl_exec($ch);

// Get HTTP Status code
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// closure cURL Session
curl_close($ch);

// Output HTTP Status code
echo "HTTP Status code是: " . $http_code;
?>

In this example, we first initialize a cURL session using curl_init() , then set the request URL, and execute the request. Next, the HTTP status code is obtained using curl_getinfo() and the status code is stored in the $http_code variable. After the session ends, we call curl_close() to close the cURL session.

2. Why can't we get the HTTP status code after curl_close() ?

curl_close() closes the current cURL session and releases its occupied resources. Once curl_close() is called, all status information related to the session will be destroyed, so the requested detailed information, including the HTTP status code, cannot be obtained through the cURL function.

Because of this, the correct time to get the HTTP status code is to complete curl_exec() execution and call curl_getinfo() before curl_close() . This ensures that the requested status information is retrieved before closing the session.

3. Get other information through curl_getinfo()

In addition to the HTTP status code, curl_getinfo() can return many other useful information. For example, return information such as the final URL of the request, the time it took, the content length, etc. Here is an example of how to get more request information:

 <?php
// initialization cURL Session
$ch = curl_init();

// Set requested URL
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/example");

// set up cURL Options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// implement cURL ask
$response = curl_exec($ch);

// Get cURL ask信息
$request_info = curl_getinfo($ch);

// closure cURL Session
curl_close($ch);

// Outputask信息
echo "HTTP Status code: " . $request_info['http_code'] . "\n";
echo "ask的 URL: " . $request_info['url'] . "\n";
echo "ask耗时: " . $request_info['total_time'] . "Second\n";
?>

Through the above code, you can obtain information such as the HTTP status code, the final request URL, and the total request time.

4. Summary

Although curl_close() closes the session and frees resources, we can still obtain the HTTP status code and other useful request information through curl_getinfo() before closing the session. Therefore, make sure to call curl_getinfo() before calling curl_close() so that the HTTP status code can be successfully obtained.

I hope that through this article, you can better understand how to use cURL to obtain HTTP status codes in PHP and avoid the problem of not being able to obtain information after closing the session.