Home Backend Development PHP Tutorial Perfect solution to failed to open stream: HTTP request failed!_PHP tutorial

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

Jul 13, 2016 pm 05:39 PM
file ge http open php5 request stream use Perfect of Bring your own solve

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

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

This can solve some people’s problems. Some people say that in php.ini, there are two options: allow_url_fopen =on (indicating that remote files can be opened through url), user_agent="PHP" (indicates which The script accesses the network. By default, remove the " ; " in front of it. Just restart the server.

But some people still have this warning message. There is still one step left to achieve a perfect solution. You have to set the user_agent in php.ini. The default user_agent in php is PHP. We 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)”

I encountered this problem at work and solved it perfectly, so I share it with everyone.
I grabbed the structural formula of chemblink in batches and found that some pictures could not be displayed after the loop, but the remote files existed.
When grabbing remote files, a warning message similar to Warning: readfile(http://www.BkJia.com/logo.gif) [function.readfile]: failed to open stream: HTTP request failed! appears. I am using

ob_start();
readfile("http://www.BkJia.com/logo.gif");
$img = ob_get_contents();
ob_end_clean();


In this way, the above error will appear from time to time during operation. I also changed other functions such as file_get_contents, but it didn't work. After checking on the Internet, I found that using the CURL method to capture 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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486273.htmlTechArticleWhen using the file_get_contents method that comes with php5 to obtain remote files, file_get_contents(): failed sometimes occurs. to open stream: HTTP request failed! This warning message. ...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

Five tips to teach you how to solve the problem of Black Shark phone not turning on!

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting?

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange?

See all articles