Perfect solution: failed to open stream: HTTP request failed!

Source: Internet
Author: User

When you use the file_get_contents method that comes with php5 to obtain remote files, sometimes file_get_contents (): failed to open stream: HTTP request failed will appear! This warning message.

Google or baidu, there are many such problems. The solution is to modify php. ini, enable allow_url_fopen, and change it to allow_url_fopen = On.

This can solve some people's problems, some people say that in php. in ini, there are two options: allow_url_fopen = on (indicating that remote files can be opened through url), user_agent = "PHP" (indicating which script is used to access the network. By default, there is "; "Remove it .) Restart the server.

However, some of them still have this warning message. If you want to use the perfect solution, you still have to set php. user_agent in ini. The default user_agent in php is PHP. We can change it to Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) to simulate the browser.

User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )"

If you encounter this problem at work and solve it perfectly, share it with you.
I captured the structure of chemblink in batches and found that some images cannot be displayed after the loop, while remote files exist.
The following error occurs when capturing remote files: Warning: readfile (http://www.bkjia.com/logo.gif) [function. readfile]: failed to open stream: HTTP request failed! Which of the following warning information is used?

Ob_start ();
Readfile ("http://www.bkjia.com/logo.gif ");
$ Img = ob_get_contents ();
Ob_end_clean ();


In this way, the above errors may occur from time to time during running, and I have changed file_get_contents and other functions, which are useless. After checking them online, I found that the CURL method will not cause errors.

$ Url = "http://www.bkjia.com/logo.gif ";
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 );
$ Img = curl_exec ($ ch );
 

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.