2023-05-12 开启多语言插件支持……

php 中file_get_contents超时问题的解决方法

php 苏 demo 2455℃ 0评论

PHP file_get_contents 获取文件 有时候会出现获取超时的情况。

尤其是在有些网络延迟的情况下,非常消耗 时间。

 

代码示例:

/** 下载图片获取大小 */
public static function downGetImageSize($url){
    $context = stream_context_create(array(
        'http' => array(
            'method'=> 'GET',
            'timeout' => 3 //超时时间,单位为秒
        )
    ));
    $content = @file_get_contents($url,0,$context);
    $image_name = rand() . '.jpg';

    $save = @file_put_contents(RUNTIME_PATH . '/Cache/' . $image_name, $content);
    $size = @getimagesize(RUNTIME_PATH.'/Cache/'.$image_name);
    @unlink(RUNTIME_PATH.'/Cache/'.$image_name);
    return !empty($size) ? array('width'=>$size['0'],'height'=>$size['1']) : '';
}
打赏

转载请注明:苏demo的别样人生 » php 中file_get_contents超时问题的解决方法

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (0)or分享 (0)