File_get_contents is particularly slow in downloading a 20 k image resource. How can this problem be solved?

Source: Internet
Author: User
{Code ...}
$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);

$urlAdd above this lineini_set('default_socket_timeout', 1);Set the default timeout value.

The image you requested, supported by the recipient's serverConnection: keep-aliveTherefore, after receiving data, PHP maintains a period of time and waits until the time-out period is reached. Therefore, it is slow when the default time-out period is set.

You can use the same code (without ini_set) to obtain the URLhttp://sfault-avatar.b0.upaiyun.com/160/666/1606661144-55fe2a534b79f_huge256The other party told PHP that the connection can be closed after receiving the output data.

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 soon ....

This should be the reason for the network. Just download an image and there is no difference in any method.

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.