2015-02-11 56 views
-2

我們的網址是遠程url.i可以通過IP地址運行,因爲「file_get_contents未能打開流:連接被拒絕」而引發錯誤。我使用此代碼。file_get_contents無法打開流:連接拒絕godaddy服務器與遠程連接服務器

代碼:

$html = file_get_contents('http://xx.xxx.xx.xx:xxxx/apps/index.php'); 
print_r($html); 
var_dump($html); 

這個錯誤是什麼?

+0

它在本地工作,任何一臺PC,但它沒有工作godaddy的服務器 – samarth 2015-02-11 09:17:18

+1

[文件可能重複\ _ GET \ _contents()連接拒絕了我自己的網站](http://stackoverflow.com/questions/3283995/file-get-contents-connection-refused-for-my-own-site) – 2015-02-11 09:19:46

+0

@洛倫茲邁爾,米沒有得到這一點。 – samarth 2015-02-11 09:24:11

回答

1

獲取頁面需要打開一個流。 事情是這樣的:

<?php 
// Create a stream 
$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
       "Cookie: foo=bar\r\n" 
) 
); 

$context = stream_context_create($opts); 

// Open the file using the HTTP headers set above 
$file = file_get_contents('http://www.example.com/', false, $context); 
?> 

僅供參考請查閱PHP.net:http://php.net/manual/en/function.file-get-contents.php

+0

file_get_contents():未能打開流:沒有這樣的文件或目錄錯誤消息 – samarth 2015-02-11 09:22:23

2

很多主機將阻止你從遠程URL加載文件出於安全原因(於allow_url_fopen在php.ini中設置)。最好使用CURL來下載文件的內容。

<?php 
$url = 'http://xx.xxx.xx.xx:xxxx/apps/index.php'; 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, false); 
$data = curl_exec($curl); 
curl_close($curl); 

編號:Get file content via PHP cURL

HTH :)

+0

當我使用此代碼時,它返回空白頁。它不顯示任何錯誤。 – samarth 2015-02-11 09:28:38

+0

var_dump($ data); ? – 2015-02-11 09:31:21