Untitled

mail@pastecode.io avatar
unknown
php
a year ago
4.0 kB
3
Indexable
Never
class WebUXCurl {

	private static function get(&$var, $default=null) {
	    return isset($var) ? $var : $default;
	}


    /** @param $site Site to load.
	 * @param $timeout Time to wait for a response, in seconds.
	 * @return object {
 	 * 		"success", //Success of the test.
	 * 		"url", //Last effective URL.
	 * 		"http_code", //Last received HTTP code.
	 * 		"redirect_count", //Number of redirects.
	 * 		"namelookup_time", //Time in milliseconds until name resolving was complete.
	 * 		"connect_time", //Time in milliseconds it took to establish the connection.
	 * 		"pretransfer_time", //Time in milliseconds from start until just before file transfer begins.
	 * 		"starttransfer_time", //Time in milliseconds until the first byte is about to be transferred.
	 * 		"redirect_time", //Time in milliseconds of all redirection steps before final transaction was started.
	 * 		"total_time", //Total transaction time in milliseconds for last transfer.
	 * 		"total_transfer_time", //=total_time+redirect_time+connect_time, in milliseconds.
	 * 		"size_download", //Total number of bytes downloaded.
	 * 		"speed_download", //Average download speed in bits per second.
     *      "size_upload", //Total number of bytes uploaded.
     *      "speed_upload" //Average upload speed in bits per second.
	 * }
	 */
    public static function execute($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
            ];
        }

    }