Phpfile_get_contents cannot obtain content

Source: Internet
Author: User
Problem description: only local files can be obtained, and the domain name is the IP address. The domain name is obtained as a null example: 1. obtain the local file {code ...} 2. obtain the IP address content {code ...} 3. domain Name content cannot be obtained {code ...} 4. if php code is changed to the curl format, you can get... problem description:
Only local files can be obtained, and the domain name is an IP address. The domain name is obtained as null.
Example:
1. obtain local files

File_get_contetns ('../1.txt'); // you can obtain

2. Obtain the IP address content

File_get_contents ('HTTP: // 1.1.1.1 '); // you can obtain

3. Domain Name content cannot be obtained

File_get_contents ('HTTP: // www.baidu.com '); // The obtained content is empty.

4. If php code is changed to the curl format, you can obtain

Environment:
Ubuntu: 13.10
Php: PHP 5.3.17
Allow_url_fopen = on is enabled.
Safe_mode: disabled

In the command line:

Curl http://www.baidu.com is okay

What is the problem?

Add:

If I direct the domain name host to 127.0.0.1, NO content can be obtained.

Reply content:

Problem description:
Only local files can be obtained, and the domain name is an IP address. The domain name is obtained as null.
Example:
1. obtain local files

File_get_contetns ('../1.txt'); // you can obtain

2. Obtain the IP address content

File_get_contents ('HTTP: // 1.1.1.1 '); // you can obtain

3. Domain Name content cannot be obtained

File_get_contents ('HTTP: // www.baidu.com '); // The obtained content is empty.

4. If php code is changed to the curl format, you can obtain

Environment:
Ubuntu: 13.10
Php: PHP 5.3.17
Allow_url_fopen = on is enabled.
Safe_mode: disabled

In the command line:

Curl http://www.baidu.com is okay

What is the problem?

Add:

If I direct the domain name host to 127.0.0.1, NO content can be obtained.

Remote use of curl for stable and efficient reading of local files using filegetcontents

You can read the PHP manual.file_get_contentsUser comments under the function will help you solve the problem.
Here are some examples:

Onglipo at hotmail dot com (08-Mar-2012)

  $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)

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

If I usefile_get_contents("Www. jbossServer. example/app1 ",...)
I will get an 'invalid hostname' from the server I'm calling.

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

And you know that your servers will 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, so the server is not rude in response.

file_get_contents()Instead of being unusable, It is a robust and mature cURL,file_get_contentsThe stability, error management, and powerful Request control functions are not available. Therefore, cURL is recommended.
The reason why the IP address can be obtained and the domain name cannot be obtained may be related to DNS. However, it should be certain that your network problems

  • One possibility is the DNS issue.

  • One possibility is that the corresponding process is not restarted after some settings are modified.

  • There is another possibility and the most possible, which is related to the php compilation parameters: because when I tried to find the information about this problem, I accidentally saw that php cannot be added.--with-curlwrapperThis compilation parameter.

Check firewall iptables settings

I don't understand why.file_get_contentsThis is an unstable way to read remote files.

If the url is correct, consider the File Permission and whether the file 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.