php file_get_contents 不能獲得內容

來源:互聯網
上載者:User
問題描述:
只能獲得本地檔案,還有網域名稱是IP 的地址, 網域名稱的方式獲得為空白
例子:
1.獲得本地檔案

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

2.獲得IP地址的內容

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

3.網域名稱形式內容不能獲得

file_get_contents('http://www.baidu.com');//獲得內容為空白

4.如果php 代碼換成 curl形式可以獲得

環境:
ubuntu:13.10
php:PHP 5.3.17
allow_url_fopen = on 是開啟的
safe_mode:disabled

在命令列下面:

curl http://www.baidu.com 是沒問題的

請大家幫忙分析下問題出在什麼地方?

補充一點:

如果我把網域名稱host 指向 127.0.0.1 也沒辦法獲得內容

回複內容:

問題描述:
只能獲得本地檔案,還有網域名稱是IP 的地址, 網域名稱的方式獲得為空白
例子:
1.獲得本地檔案

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

2.獲得IP地址的內容

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

3.網域名稱形式內容不能獲得

file_get_contents('http://www.baidu.com');//獲得內容為空白

4.如果php 代碼換成 curl形式可以獲得

環境:
ubuntu:13.10
php:PHP 5.3.17
allow_url_fopen = on 是開啟的
safe_mode:disabled

在命令列下面:

curl http://www.baidu.com 是沒問題的

請大家幫忙分析下問題出在什麼地方?

補充一點:

如果我把網域名稱host 指向 127.0.0.1 也沒辦法獲得內容

讀取遠程用curl穩定且高效 讀取本地檔案用filegetcontents

題主可以讀讀PHP手冊file_get_contents函數下的使用者評論,會對你解決問題有協助。
舉幾個栗子:

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 hostnames instead straight IP with some server destinations.

If i use file_get_contents("www.jbossServer.example/app1",...)
i will get an 'Invalid hostname' from the server i'm calling.

This is because file_get_contents probably will 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 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()不是不能用,而是相比健壯成熟的cURL,file_get_contents的穩定性,錯誤管理,還有強大的請求控制等功能都沒有,所以這裡,大家都建議用cURL。
至於為什麼IP可以獲得而網域名稱獲得不了,或許和DNS有關。不過應該可以確定的是你的網路問題

  • 一個可能性是 DNS 的問題

  • 一個可能性是改了一些設定後沒有重啟相應進程

  • 還有一個可能,也是最可能的,和 php 的編譯參數有關:因為我在試著尋找這個問題的資料的時候無意間看到了 http://stackoverflow.com/questions/4172860/file-get-contents-returns-empty-string,裡面提到,安裝 php 時不能加上 --with-curlwrapper 這個編譯參數。

檢查防火牆iptables設定

非常不理解,為什麼要用file_get_contents這麼不穩定的方式讀取遠程檔案。

如果url沒錯,考慮下檔案許可權和是否可讀的問題

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.