How to Retrieve Detailed cURL Request Info in PHP with curl_getinfo
This guide explains how PHP's curl_getinfo function returns an associative array of request details, lists the most useful options such as effective URL and HTTP status code, and provides a complete example showing how to extract and display these metrics after a cURL request.
Using curl_getinfo() to retrieve request details
curl_getinfo() returns an associative array with information about a completed cURL request. It should be called after curl_exec() and before curl_close().
Commonly accessed fields
url – final effective URL (CURLOPT_EFFECTIVE_URL).
http_code – HTTP response status code (CURLOPT_HTTP_CODE, also available as CURLOPT_RESPONSE_CODE from libcurl 7.10.8).
total_time – total transaction time in seconds (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
$response = curl_exec($curl);
// Retrieve request information
$info = curl_getinfo($curl);
// Display selected metrics
echo "Request URL: " . $info['url'] . PHP_EOL;
echo "HTTP status code: " . $info['http_code'] . PHP_EOL;
echo "Total time: " . $info['total_time'] . " seconds" . PHP_EOL;
echo "Download size: " . $info['download_content_length'] . " bytes" . PHP_EOL;
echo "Upload size: " . $info['upload_content_length'] . " bytes" . PHP_EOL;
// Close the handle
curl_close($curl);The script follows the typical workflow: initialize, set options, execute, call curl_getinfo() to obtain metrics, output the values, and finally close the session. These metrics are valuable for debugging, performance monitoring, or building tools such as web crawlers and API clients.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
