How to get PHP cURL info such as execute time and number of HTTP request made

295 Views Asked by At

I am trying to calculate

  1. No of HTTP request made by cURL to example.com
  2. Total time taken for cURL to get string from example.com
  3. No of Redirect count.

Code :

function file_get_contents_curl($url) {
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);   
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    // Get the content type
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_exec($ch);
    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

    // Get the content
    curl_setopt($ch, CURLOPT_NOBODY, 0);
    $data = curl_exec($ch);
    
    // Stats 
    $http_request = curl_getinfo($ch, CURLINFO_NUM_CONNECTS);
    $total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
    $request_count = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) + 1;
    $connect_time = curl_getinfo($ch, CURLINFO_CONNECT_TIME);
    curl_close($ch); 

    // Set the content type header
    header('Content-Type:' . $content_type);

    echo "HTTP Request: " . $http_request . "<br>";
    echo "Total Time: " . $total_time . "<br>";
    echo "Redirect count: " . $request_count . "<br>";
    echo "Connection time: " . $connect_time . "<br>";

    return $data;
}

$homepage = file_get_contents_curl("https://example.com");
echo $homepage;

Error : $http_request value returning 0

2

There are 2 best solutions below

0
Mehul Kumar On

Edited with one curl_exec

    function file_get_contents_curl($url) {
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);   
    curl_setopt($ch, CURLOPT_HEADER, true); // Include response headers

    $response = curl_exec($ch);

    // Get the content type from the response headers
    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

    // Extract the data from the response (after the headers)
    $data = substr($response, curl_getinfo($ch, CURLINFO_HEADER_SIZE));

    curl_close($ch);

    // Set the content type header
    header('Content-Type:' . $content_type);

    return $data;
}

$homepage = file_get_contents_curl("https://example.com");
echo $homepage;
0
JMP On

From the PHP Manual:

CURLOPT_NOBODY true to exclude the body from the output. Request method is then set to HEAD. Changing this to false does not change it to GET.

So after you have set it to TRUE, the request method is HEAD. Setting it back to FALSE doesn't set the request method back to GET - you need to do this explicitly:

curl_setopt($ch, CURLOPT_HTTPGET, TRUE);

before your second call.