file_get_contents Download a 20K picture resource is very slow, how to solve

Source: Internet
Author: User
$url = 'http://wx.qlogo.cn/mmopen/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic4JSDngEVjVo10BoiaPd0iciaOb/0';$content = file_get_contents($url);file_put_contents('uploads/a.jpg', $content);

Reply content:

$url = 'http://wx.qlogo.cn/mmopen/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic4JSDngEVjVo10BoiaPd0iciaOb/0';$content = file_get_contents($url);file_put_contents('uploads/a.jpg', $content);

$urlThis line adds the ini_set('default_socket_timeout', 1); default time-out to be set.

You requested this image, the other side of the server support Connection: keep-alive , so PHP after receiving data, maintained for a period of time, until the timeout to return, so in the case of setting the default time-out is slower.

You can use the same code (do not use ini_set), get this URL http://sfault-avatar.b0.upaiyun.com/160/666/1606661144-55fe2a534b79f_huge256 , quickly back, because the other side told PHP, after the output of the data received, the connection can be closed.

function curl_file_get_contents($durl){   $ch = curl_init();   curl_setopt($ch, CURLOPT_URL, $durl);   curl_setopt($ch, CURLOPT_TIMEOUT, 2);   curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);   curl_setopt($ch, CURLOPT_REFERER,_REFERER_);   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   $r = curl_exec($ch);   curl_close($ch);   return $r; }$url = 'http://wx.qlogo.cn/mmopen/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic4JSDngEVjVo10BoiaPd0iciaOb/0';$a = curl_file_get_contents($url);file_put_contents('uploads/2.jpg', $a);

This will be quick ....

This should be the network reason, just download a picture, with what method can not feel the difference at all

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.