Current Location: Home> Latest Articles> PHP curl_getinfo() Function Explained: How to Retrieve cURL Request Information

PHP curl_getinfo() Function Explained: How to Retrieve cURL Request Information

M66 2025-07-15

PHP curl_getinfo() Function Explained: How to Retrieve cURL Request Information

In PHP, cURL is a powerful tool used for sending and receiving data across various protocols, such as HTTP, FTP, SMTP, and more. When making cURL requests, you might need to retrieve specific details like the request URL, server response status code, request time, etc. This is where the curl_getinfo() function comes in handy, allowing you to obtain such information.

Overview of curl_getinfo() Function

The curl_getinfo() function returns an associative array containing information related to the cURL request. You can specify different options to retrieve various pieces of information. Below are some common options and their meanings:

  • CURLOPT_EFFECTIVE_URL: Returns the effective URL of the request as a string.
  • CURLOPT_HTTP_CODE: Returns the HTTP status code of the server's response as an integer.
  • CURLOPT_RESPONSE_CODE: Returns the HTTP response code (available from cURL version 7.10.8 and above).
  • CURLOPT_TOTAL_TIME: Returns the total time taken for the entire request (in seconds).
  • CURLOPT_CONTENT_LENGTH_DOWNLOAD: Returns the number of bytes downloaded.
  • CURLOPT_CONTENT_LENGTH_UPLOAD: Returns the number of bytes uploaded.

How to Use curl_getinfo() to Retrieve Request Information

Here’s an example of using the curl_getinfo() function to retrieve cURL request information:

// Create a cURL resource
$curl = curl_init();

// Set the request URL
curl_setopt($curl, CURLOPT_URL, "https://www.example.com");

// Execute the request
$response = curl_exec($curl);

// Retrieve request-related information
$info = curl_getinfo($curl);

// Output the request URL
echo "Requested URL: " . $info['url'] . "<br>";

// Output the HTTP status code
echo "HTTP Status Code: " . $info['http_code'] . "<br>";

// Output the total time for the request
echo "Total Time: " . $info['total_time'] . " seconds<br>";

// Output the downloaded content length
echo "Downloaded Content Length: " . $info['download_content_length'] . "<br>";

// Output the uploaded content length
echo "Uploaded Content Length: " . $info['upload_content_length'] . "<br>";

// Close the cURL resource
curl_close($curl);

In the code above, we first create a cURL resource and set the request URL. After executing the request, we use curl_getinfo() to retrieve information about the request and output it to the page. Finally, we close the cURL resource.

Summary

By using the curl_getinfo() function, developers can easily retrieve a variety of information related to cURL requests. Whether you're developing a web crawler, calling an API, or sending HTTP requests, understanding how to use this function will significantly improve your efficiency.

In conclusion, the curl_getinfo() function is extremely useful in PHP development, particularly when working with cURL for data interactions. It allows us to retrieve detailed request information and better control and handle requests.