How to Retrieve Detailed cURL Request Info in PHP with curl_getinfo

This guide explains how to use PHP's curl_getinfo() function to obtain request details such as effective URL, HTTP status code, total time, and content lengths, and provides a complete example demonstrating initialization, execution, information retrieval, and output of these metrics.

php Courses
php Courses
php Courses
How to Retrieve Detailed cURL Request Info in PHP with curl_getinfo

Retrieving cURL request details with curl_getinfo() in PHP

The curl_getinfo() function returns an associative array that contains a wide range of metrics about a completed cURL transfer. By querying specific keys you can obtain the effective request URL, HTTP status code, timing information, and transferred byte counts.

Commonly used information keys

url – The final request URL after redirects ( CURLOPT_EFFECTIVE_URL).

http_code – HTTP response status code returned by the server ( CURLOPT_HTTP_CODE, also available as CURLOPT_RESPONSE_CODE from cURL 7.10.8).

total_time – Total transaction time in seconds (float) ( CURLOPT_TOTAL_TIME).

download_content_length – Number of bytes downloaded ( CURLOPT_CONTENT_LENGTH_DOWNLOAD).

upload_content_length – Number of bytes uploaded ( CURLOPT_CONTENT_LENGTH_UPLOAD).

Complete example

// Initialize a cURL session
$curl = curl_init();

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

// Execute the request (true on success, false on failure)
$response = curl_exec($curl);

// Retrieve detailed information about the transfer
$info = curl_getinfo($curl);

// Display selected metrics
echo "Request URL: " . $info['url'] . "
";
echo "HTTP status code: " . $info['http_code'] . "
";
echo "Total time: " . $info['total_time'] . " seconds
";
echo "Download size: " . $info['download_content_length'] . " bytes
";
echo "Upload size: " . $info['upload_content_length'] . " bytes
";

// Close the cURL handle to free resources
curl_close($curl);

This script demonstrates the typical workflow: initialize the cURL handle, configure the request URL, execute the transfer, call curl_getinfo() to obtain performance and transfer metrics, output the values, and finally close the handle. Accessing these details is essential for debugging, performance monitoring, and building robust HTTP clients, API wrappers, or web crawlers in PHP.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPcURLcurl_getinfo
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.