Capturing the time taken to download using wget

时间:2016-04-04 18:56:02

标签: php wget

If I run the following command through the terminal:

wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" WEBSITEURL.COM -O TestFile.html

I get a file with the contents of the page at WEBSITEURL.COM. The file is desired, however, I would also like to get 1) file size and 2) elapsed download time put into variables.

I can get the file with PHP using the following:

$getFile = exec("wget --user-agent=\"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3\" $url -O Downloaded-$url.html");

The file size I can easily get but it's the elapsed download time to get that file in the first place that's eluding me.

Perhaps there's a better option than wget I might use...?

1 个答案:

答案 0 :(得分:0)

这有效:

<?php

$loadInstance = new LoadFileFromHTML();
echo "Duration:".$loadInstance->loadFileAndGetTime( "your-file", "http://www.google.com" );

class LoadFileFromHTML
{
    public function loadFileAndGetTime( $fileName, $fromUrl )
    {
        $aCurlOptions = array(
            CURLOPT_URL             => $fromUrl,
            CURLOPT_USERAGENT       => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3",
            CURLOPT_RETURNTRANSFER  => 1,
            );

        $curlInstance = curl_init();
        curl_setopt_array($curlInstance, $aCurlOptions);

        $start        = microtime( TRUE );
        $returnedData = curl_exec($curlInstance);
        $duration     = microtime( TRUE ) - $start;

        curl_close($curlInstance);
        file_put_contents( $fileName, $returnedData );
        return $duration;
    }
}
?>

注意:

  1. 如果可以写入输出文件,则不进行检查。
  2. 您必须在CURL选项中解决SSL证书问题。
  3. 未对提供的网址进行检查 - 如果正确无误。
  4. 它以秒为单位返回所需的时间。