【问题标题】:file_get_contents() with context to use http/1.1 significantly slow download speeds带有上下文的 file_get_contents() 使用 http/1.1 下载速度显着降低
【发布时间】:2010-08-15 01:32:05
【问题描述】:

使用每个图像下载下方的代码) file_get_contents() )平均需要 8-15 秒.....

如果我不在 file_get_contents() 上使用上下文,则图像下载不到一秒。

如果我将 $opts 更改为,那么我将在没有上下文的情况下获得与 file_get_contents() 相同的性能,这需要大约 13 秒来处理 2,500 个图像x。

$opts = array(
    'http'=>array(
        'protocol_version'=>'1.1',
        'method'=>'GET',
        'header'=>array(
            'Connection: close'
        ),
        'user_agent'=>'Image Resizer'
     )
); 

转载:

    $start_time = mktime();
$products = array(
        array( 'code'=>'A123', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A124', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A125', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A126', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A127', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A128', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A134', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A135', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A146', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' ),
        array( 'code'=>'A165', 'image_url'=>'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png' )
    );

    if ( count( $products ) > 0 ) {
        $opts = array(
            'http'=>array(
                'protocol_version'=>'1.1',
                'method'=>'GET',
                'user_agent'=>'Image Resizer'
            )
        ); 
        $context = stream_context_create($opts);
        $def_width = 100;
        $max_width  = $def_width;
        foreach( $products as $product ) {
            $code = $product['code'];
            $folder = substr( $code, 0, 3 );
            echo( 'Looking at: ' .$product['code'] ."<br />" );
            $file = '/tmp/' .$folder .'/' .$code .'_' .$def_width .'.jpg';
            $filemtime = @filemtime($file);
            $gen_file = true;
            if ( $filemtime !== false ) {
                $file_age = (time() - $filemtime);
                if ( $file_age <= 300 ) {
                    $gen_file = false;
                }
            }
            echo( '&nbsp;&nbsp;&nbsp;&nbsp;File not cached or cached file has expired<br />' );
            if ( $gen_file ) {
                echo( '&nbsp;&nbsp;&nbsp;&nbsp;Getting File...');
                $imgStr = file_get_contents( $product['image_url'], false, $context );
                $img = @imagecreatefromstring($imgStr);
                if ( is_resource( $img ) ) {
                    echo( 'DONE' .'<br />' );
                    $image_x = imagesx($img);
                    $image_y = imagesy($img);
                    if ( $def_width >= $image_x ) {
                        $def_width = $image_x;
                    }
                    echo( '&nbsp;&nbsp;&nbsp;&nbsp;Calculating Scale<br />' );
                    $ts = min( $max_width/$image_x,$max_width/$image_y);
                    $thumbhght = $ts * $image_y;
                    $thumbwth = $ts * $image_x;

                    $thumb_image_resized = imagecreatetruecolor( $thumbwth, $thumbhght);
                    imagecopyresampled($thumb_image_resized, $img, 0, 0, 0, 0, $thumbwth, $thumbhght, $image_x, $image_y );
                    echo( '&nbsp;&nbsp;&nbsp;&nbsp;Checking For Directory<br />' );
                    if ( !is_dir( '/tmp/' .$folder ) ) {
                        mkdir( '/tmp/' .$folder );
                    }
                    echo( '&nbsp;&nbsp;&nbsp;&nbsp;Writing File<br />' );
                    $new_file = '/tmp/' .$folder .'/' .$code .'_' .$def_width .'.jpg';

                    imagejpeg( $thumb_image_resized, $new_file, 100);
                    echo( '&nbsp;&nbsp;&nbsp;&nbsp;DONE<br />' );

                    imagedestroy($img);
                    imagedestroy($thumb_image_resized);
                } else {
                    echo( 'Problem Getting Image<br />' );
                    die();
                }
            } else {
                echo( '&nbsp;&nbsp;&nbsp;&nbsp;Already Exists<br />' );
            }
        }
    }
    $end_time = mktime();
    echo( 'Completed In...' .($end_time - $start_time ) .' seconds(s)<br />' );

【问题讨论】:

    标签: php http http-1.1


    【解决方案1】:

    HTTP 1.1 请求默认是流水线的。如果您不“连接:关闭”,则假定“连接:保持活动”,然后您必须等待连接超时(因为您从未明确关闭它)才能开始下一个循环。

    【讨论】:

    • 谢谢!在 HTTP 1.0 上花费 0.15 秒的请求在 HTTP 1.1 上花费了至少 5 秒。一个简单的 `header("Connection: close");修好了!
    【解决方案2】:

    您的上下文告诉 file_get_contents() 每次都关闭 HTTP 连接。也许这就是代码需要这么长时间的原因,因为它会多次关闭并重新打开连接?我不熟悉 file_get_contents() 的内部结构,但您可以调整上下文以对除最后一个连接以外的所有连接使用“Connection: keep-alive”,对最后一个连接使用“Connection: close”。

    【讨论】:

    • 我希望file_get_contents() 无论如何都会关闭它的连接——它在从磁盘读取文件时会关闭文件句柄。如果你想要性能,cURL 是一个更好的选择。您可以简单地重复使用相同的 curl 句柄;如果我没记错的话,默认情况下连接保持打开状态。
    • 我同意你关于使用 cURL 的观点,除非 dorgan 有理由必须使用 file_get_contents()。
    猜你喜欢
    • 2018-06-18
    • 2020-12-28
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-19
    • 2020-05-15
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多