1

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

JMP
  • 4,417
  • 17
  • 30
  • 41
Mehul Kumar
  • 461
  • 8
  • Number of HTTP requests made, is not the same thing as number of connections. – CBroe Mar 02 '23 at 11:12
  • @CBroe than how to check that? – Mehul Kumar Mar 02 '23 at 11:19
  • 1
    Check what? You asked about _three_ figures, your are outputting _four_ values in your code - so how are we supposed to know now, what "that" is supposed to be? – CBroe Mar 02 '23 at 11:23
  • @CBroe Two output is related to `time`. I am trying to figure out how to track **No Of HTTP Request made to example.com** – Mehul Kumar Mar 02 '23 at 11:27
  • Well that would be the number of redirects plus one, wouldn't it? So why are you asking about what you already _have_ in your code? Does the number you are getting appear to be wrong ...? – CBroe Mar 02 '23 at 11:36
  • @CBroe my client said my above cURL code makes `2x HTTP request` to `example.com`. 1st to get `body string` and second to get `MIME Type`. **My output for redirect count is 1**. So, Should i consider one HTTP request made to example.com? – Mehul Kumar Mar 02 '23 at 11:40
  • 1
    Of course you are making two requests here, you are calling `curl_exec($ch)` two times. Why does your client need to tell _you_, what _you_ are actually doing? – CBroe Mar 02 '23 at 11:46
  • _"My output for redirect count is 1"_ - you are only looking at the counts for _one_ of the _two_ requests you are making, so all this really means rather little to begin with. – CBroe Mar 02 '23 at 11:47
  • @CBroe Like you mentioned my code makes **2xHTTP request**. How can i calculate using the PHP and display in output. Show, i can show my client. ? – Mehul Kumar Mar 02 '23 at 11:51
  • Well get the relevant numbers after _each_ request? I really have no idea what you want with all this. Why aren't you making just _one_ request in the first place? – CBroe Mar 02 '23 at 11:54
  • @CBroe **Why aren't you making just one request in the first place** - I am trying same. To modify my code, first i need a detail that my current code making how many `HTTP Request` and there i am finding struggle to find no of request. – Mehul Kumar Mar 02 '23 at 11:57
  • 1
    The fact that you _are_ calling `curl_exec` twice, means that you _are_ making two requests. It does not need any additional checking on redirect counts etc., to realize that. – CBroe Mar 02 '23 at 12:00
  • @CBroe Got it. It needed for getting MIME type. – Mehul Kumar Mar 02 '23 at 12:02
  • No, it's not needed. Just stop making that request with `CURLOPT_NOBODY` set to true altogether. Get `CURLINFO_CONTENT_TYPE` for the _one_ request that gets the full resources. – CBroe Mar 02 '23 at 12:05
  • @CBroe I posted in answer. is this correct right?. One HTTP request – Mehul Kumar Mar 02 '23 at 12:10

2 Answers2

0

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;
Mehul Kumar
  • 461
  • 8
0

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.

JMP
  • 4,417
  • 17
  • 30
  • 41