Untitled

mail@pastecode.io avatar
unknown
php
a year ago
2.4 kB
2
Indexable
Never
<?php
$url="https://twitter.com/home"; 
$timeout=10;

function fetchUrlInfo($url, $timeout) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER,         1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,  1);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');

curl_exec($ch);
if ($errno = curl_errno($ch))
{
    $error_message = curl_strerror($errno);
    curl_close($ch);
    return [
        'success' => false,
        'msg'     => $error_message,
        'errno'   => $errno
    ];
}
else
{
    $info = curl_getinfo($ch);
    curl_close($ch);
    return  [
        'success'        => true,
        'url'            => $info['url'],
        'http_code'      => $info['http_code'],
        'redirect_count' => $info['redirect_count'],
        'namelookup_time'=> floor($info['namelookup_time']*1000), //Time in mili seconds until name resolving was complete
        'connect_time'   => floor($info['connect_time']*1000), //Time in mili seconds it took to establish the connection
        'pretransfer_time'   => floor($info['pretransfer_time']*1000), // Time in mili seconds from start until just before file transfer begins
        'starttransfer_time' => floor($info['starttransfer_time']*1000), //Time in mili seconds until the first byte is about to be transferred
        'redirect_time'      => floor($info['redirect_time']*1000), //Time in mili seconds of all redirection steps before final transaction was started
        'total_time'         => floor($info['total_time']*1000), //Total transaction mili time in seconds for last transfer
        'total_transfer_time'=> floor($info['total_time']*1000)+floor($info['redirect_time']*1000)+floor($info['connect_time']*1000),
        'size_download'  => $info['size_download'], //Total number of bytes downloaded
        'speed_download' => $info['speed_download'], //Average download speed
        'size_upload' => $info['size_upload'],	//Total number of bytes uploaded
        'speed_upload' => $info['speed_upload'],	//Average upload speed
    ];
}}

$info = fetchUrlInfo($url,$timeout);

print_r($info);


?>