問題描述:
只能獲得本地檔案,還有網域名稱是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有關。不過應該可以確定的是你的網路問題
檢查防火牆iptables設定
非常不理解,為什麼要用file_get_contents這麼不穩定的方式讀取遠程檔案。
如果url沒錯,考慮下檔案許可權和是否可讀的問題