PHP file_get_contents cannot get content

Source: Internet
Author: User
Problem Description:
can only get local files, and the domain name is IP address, the way to get the domain name is empty
Example:
1. Get Local Files

file_get_contetns('../1.txt');//可以获得

2. Get the contents of the IP address

file_get_contents('http://1.1.1.1');//可以获得

3. Domain name Form content cannot be obtained

file_get_contents('http://www.baidu.com');//获得内容为空

4. If the PHP code is replaced with curl, you can get

Environment:
ubuntu:13.10
php:php 5.3.17
Allow_url_fopen = On is open
Safe_mode:disabled

Below the command line:

Curl http://www.baidu.com is no problem.

Please help to analyze where the problem is?

Add one point:

If I point the domain host to 127.0.0.1, there's no way to get content.

Reply content:

Problem Description:
can only get local files, and the domain name is IP address, the way to get the domain name is empty
Example:
1. Get Local Files

file_get_contetns('../1.txt');//可以获得

2. Get the contents of the IP address

file_get_contents('http://1.1.1.1');//可以获得

3. Domain name Form content cannot be obtained

file_get_contents('http://www.baidu.com');//获得内容为空

4. If the PHP code is replaced with curl, you can get

Environment:
ubuntu:13.10
php:php 5.3.17
Allow_url_fopen = On is open
Safe_mode:disabled

Below the command line:

Curl http://www.baidu.com is no problem.

Please help to analyze where the problem is?

Add one point:

If I point the domain host to 127.0.0.1, there's no way to get content.

Read remote with Curl stable and efficiently read local files with filegetcontents

The owner can read the file_get_contents user comments under the PHP manual function, which will help you solve the problem.
Give some chestnuts:

Onglipo at hotmail dot com (08-mar-2012 01:27)

  $header = file_get_contents('http://www.example.com/faq.jsp');  echo $header;   Fails with 500 Internal Server Error.   $opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));  $context = stream_context_create($opts);  $header = file_get_contents('http://www.example.com/faq.jsp',false,$context);  //$header = file_get_contents('http://www.polama.com/faq.jsp');  echo $header;

works!

godwraith01 at yahoo dot com (11-oct-2011 12:16)

I experienced a problem in using the hostnames instead straight IP with some server destinations.

If I use file_get_contents ("Www.jbossServer.example/app1",...)
I'll get a ' Invalid hostname ' from the server I ' m calling.

This is because file_get_contents probably would rewrite your request after getting the IP, obtaining the same thing as:
file_get_contents("Xxx.yyy.www.zzz/app1",...)

And you know that many servers would deny you access if you go through IP addressing in the request.

With CURL This problem doesn ' t exists. it resolves the hostname leaving the request as you set it up, so the server was not rude in response.

file_get_contents()Not to be used, but rather than robust mature curl, file_get_contents stability, error management, and powerful request control functions are not, so here, we all recommend using curl.
As for why IP can be obtained and the domain name cannot be obtained, it may be related to DNS. But it should be certain that you have a network problem.

    • One possibility is the DNS problem

    • One possibility is to change some settings without restarting the corresponding process

    • Another possibility, and most likely, is related to the compiler parameters of PHP: I accidentally saw http://stackoverflow.com/questions/4172860/when I tried to find the information about the problem. File-get-contents-returns-empty-string, it is mentioned that this compilation parameter cannot be added when installing PHP --with-curlwrapper .

Check firewall iptables settings

It is very incomprehensible why you should file_get_contents read remote files in such an unstable way.

If the URL is correct, consider the issue of file permissions and whether it is readable

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