Using curl_getinfo() in PHP to Retrieve Request Information
This article explains how to use PHP's curl_getinfo() function to retrieve details such as the effective URL, HTTP status code, total execution time, and content lengths of a cURL request, and provides a complete example demonstrating its usage.
cURL is a powerful tool in PHP for sending and receiving data over various protocols such as HTTP, FTP, and SMTP. After sending a request, you may need to obtain information about the request, which can be done using the curl_getinfo() function.
The curl_getinfo() function returns an associative array containing request-related information. By specifying different options, you can retrieve specific data. Common options include: CURLOPT_EFFECTIVE_URL: returns the request URL as a string. CURLOPT_HTTP_CODE: returns the HTTP status code as an integer. CURLOPT_RESPONSE_CODE: returns the HTTP status code (available since cURL 7.10.8). CURLOPT_TOTAL_TIME: returns the total time of the request in seconds as a float. CURLOPT_CONTENT_LENGTH_DOWNLOAD: returns the number of bytes downloaded. CURLOPT_CONTENT_LENGTH_UPLOAD: returns the number of bytes uploaded.
The following example demonstrates how to use curl_getinfo():
// 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);
// Get request information
$info = curl_getinfo($curl);
// Output the request URL
echo "Request URL: " . $info['url'] . "<br>";
// Output the HTTP status code
echo "HTTP status code: " . $info['http_code'] . "<br>";
// Output total time
echo "Total time: " . $info['total_time'] . " seconds<br>";
// Output download content length
echo "Download bytes: " . $info['download_content_length'] . "<br>";
// Output upload content length
echo "Upload bytes: " . $info['upload_content_length'] . "<br>";
// Close the cURL resource
curl_close($curl);This code creates a cURL handle, sets the target URL, executes the request, retrieves various pieces of information with curl_getinfo(), prints them, and finally closes the handle.
Using curl_getinfo() allows developers to easily access request details, which is useful for web crawlers, API calls, or any HTTP interaction in PHP.
In summary, curl_getinfo() is a practical function that helps you obtain detailed information about a cURL request, enhancing control and maintainability in PHP applications.
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.
