Php fopen function cannot obtain remote address content

Source: Internet
Author: User

Fopen functions are mostly used to read and write files in php, but are sometimes used to obtain files from remote servers. However, when using fopen to read remote files, you must enable allow_url_fopen.

Solution Process

First, the DNS problem is ruled out, because everything except these functions works normally. Although it is a URL with a domain name, The gethostbyname () function returns a correct result. Then I thought about the configuration of php. ini -- but I found that allow_url_fopen has been enabled. Later, I asked Google for help. Some people mentioned SELINUX. But I didn't open SELINUX at all. Continue to Google and find StackOverflow

The Code is as follows: Copy code

$ File = fopen ('HTTP: // www.google.com/', 'rb ');
Var_dump (stream_get_meta_data ($ file ));

/*
Output result:
Array (10 ){
["Wrapper_data"] =>
Array (2 ){
["Headers"] =>
Array (0 ){
}
["Readbuf"] =>
Resource (38) of type (stream)
}

["Wrapper_type"] =>
String (4) "cURL"

["Stream_type"] =>
String (4) "cURL"

["Mode"] =>
String (2) "rb"

["Unread_bytes"] =>
Int (0)

["Seekable"] =>
Bool (false)

["Uri"] =>
String (23) "http://www.google.com /"

["Timed_out"] =>
Bool (false)

["Blocked"] =>
Bool (true)

["Eof"] =>
Bool (false)

}*/

To use functions such as fopen, getimagesize, or include to open a url, you need. ini settings, usually set allow_url_fopen to on to allow fopen url, set allow_url_include to on to allow include/require url, but it may not work in local test environment.


Allow_url_fopen = on

Whether to allow the treatment of URLs (like http: // or ftp: //) as files.

Allow_url_include = on

Whether to allow include/require to open URLs (like http: // or ftp: //) as files.

In the local wamp test environment, after this setting, fopen can open the remote address normally, but an error will be reported in case of a local address, such

The Code is as follows: Copy code
1 fopen ("http: // localhost/myfile. php", "r ");

An error is reported after the maximum execution time of the script set in php. ini is exceeded, indicating that the file does not exist. This won't happen on the online server, but it works normally if you replace localhost with 127.0.0.1.

According to the situation, the problem lies in DNS resolution. It is said that localhost has been automatically mapped to 127.0.0.1. In fact, accessing http: // localhost and accessing http: // 127.0.0.1 also reach the same address.

The solution is to check the Windows host file, which is usually located in the system32 directory. A system disk is the host path of drive C as follows:

The Code is as follows: Copy code

C:/Windows/System32/drivers/etc/hosts

Open the hosts file and use notepad, notepad ++, and other tools.

Remove the # Above 127.0.0.1.

The Code is as follows: Copy code

# Localhost name resolution is handled within DNS itself.
#127.0.0.1 localhost

What is the use of a url as a file?
For example, you can pass a value to an include file.

The Code is as follows: Copy code

<? Php include 'HTTP: // yourdomain.com/example. inc. php? Foo = 1 & bar = 2';?>

In example. inc. php

The Code is as follows: Copy code

<? Php
Var_dump ($ _ GET ['foo']);
Var_dump ($ _ GET ['bar']);
?>

Running result

String (1) "1" string (1) "2"

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.