The phpfopen function cannot obtain the 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.

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:

  1. $ File = fopen ('http: // www.phpfensi.com/', 'RB ');
  2. Var_dump (stream_get_meta_data ($ file ));
  3. /*

Output result:

  1. Array (10 ){
  2. ["Wrapper_data"] =>
  3. Array (2 ){
  4. ["Headers"] =>
  5. Array (0 ){
  6. }
  7. ["Readbuf"] =>
  8. Resource (38) of type (stream)
  9. }
  10. ["Wrapper_type"] =>
  11. String (4) "cURL"
  12. ["Stream_type"] =>
  13. String (4) "cURL"
  14. ["Mode"] =>
  15. String (2) "rb"
  16. ["Unread_bytes"] =>
  17. Int (0)
  18. ["Seekable"] =>
  19. Bool (false)
  20. ["Uri"] =>
  21. String (23) "http://www.phpfensi.com /"
  22. ["Timed_out"] =>
  23. Bool (false)
  24. ["Blocked"] =>
  25. Bool (true)
  26. ["Eof"] =>
  27. Bool (false)
  28. }*/

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.

  1. Allow_url_fopen = on
  2. Whether to allow the treatment of URLs (like http: // or ftp: //) as files.
  3. Allow_url_include = on
  4. 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 is reported when the local address is encountered. for example, the code is as follows:

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:

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

Open the hosts file, use notepad, notepad ++, and other tools to remove the # before 127.0.0.1. the code is as follows:

  1. # Localhost name resolution is handled within DNS itself.
  2. #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.

In example. inc. php

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.