2011-09-07 30 views
-3

工作可能重複:
file_get_content failed to open stream: Connection refused的file_get_contents()無法在Linux中

file_get_content功能不能正常工作在Linux上vqs332.pair.com 2.6.32-33服務器。要麼我需要改變我的php.ini中的任何設置或其他問題。 我使用這個函數從地址(動態)獲取緯度和經度。

file_get_contents("http://maps.google.com/maps/api/geocode/json?address=24411+Health+Center+Dr+Suite+200&sensor=false"); 

Plz指導我。

+1

請詳細說明「不工作」。請添加您收到的任何錯誤消息(您已啓用錯誤日誌記錄和error_reporting(E_ALL),對不對?)。請解釋你期望得到什麼,以及你實際得到了什麼。 – MarkR

+0

你在代理的背後嗎? – jbrond

+0

您是否檢查過文件和目錄中的權限? – Evernoob

回答

1

問題可能是您的主機禁用了allow_url_fopen。這在共享主機環境中很常見,並且是一種明智的安全措施。

爲了解決這個問題,你必須使用cURL將文件下載到本地路徑並在本地使用。

例如

// Remote file we want, and the local file name to use as a temp file 
$url = 'http://maps.google.com/maps/api/geocode/json?address=24411+Health+Center+Dr+Suite+200&sensor=false'; 
$localfile = 'mytempfilename.ext'; 

// Let's go cURLing... 
$ch = curl_init($url); 
$fp = fopen($localfile,'w'); 

curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

curl_exec($ch); 
curl_close($ch); 
fclose($fp); 

// Get the data into memory and delete the temp file 
$filedata = file_get_contents($localfile); 
unlink($localfile); 
+0

'問題是你的主機有allow_url_fopen'。事實上,你不能肯定地說。 –

+0

@Col Shrapnel允許,但必須有90%的可能性是問題所在,因爲這些問題出現的頻率以及答案的頻率如何?我將用'可能'重新將它記下來 – DaveRandom

+0

感謝您的意見,我在我的.htcaess文件中將allow_url_fopen更改爲On。它的工作現在。 – Viralk