I am trying to calculate
- No of HTTP request made by cURL to example.com
- Total time taken for cURL to get string from example.com
- 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