2014-09-19 29 views
0

我有一個問題開展工作,我需要從一個url獲取內容PHP的file_get_contents不使用<code>file_get_contents</code></p> <p>當服務器

$header=array(); 
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$header[] = "Cache-Control: max-age=0"; 
$header[] = "Connection: keep-alive"; 
$header[] = "Keep-Alive: 300"; 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
$header[] = "Accept-Language: en-us,en;q=0.5"; 
$header[] = "Pragma: "; 
$opts = array(
       'http'=>array(
          'method'=>"GET", 
          'header'=>implode('\r\n',$header), 
          'proxy' => 'proxy_address', 
          'request_fulluri' => True 
          ) 
        ); 

$context = stream_context_create($opts); 
$file = file_get_contents($url, false, $context); 
echo $file; 

錯誤消息說:file_get_contents(my url) [function.file-get-contents]: failed to open stream: Connection timed out

我嘗試了curl請求,但得到相同的迴應。我怎麼解決這個問題?

+0

您的託管公司可能會阻止傳出請求,請與他們聯繫。 – Maerlyn 2014-09-19 09:17:37

+0

你是否嘗試過不同主機上的代碼?它看起來沒問題,只有我能想到的是你的主機/外部網站沒有響應。 – JakeSteam 2014-09-19 09:18:20

+0

與其他網站代碼工作正確,我認爲網站,我嘗試解析阻止我的請求(爲什麼我添加代理),但它沒有幫助:( – Kandrat 2014-09-19 09:37:21

回答

0

嘗試超時設置添加到您的OPTS:

$opts = array(
       'http'=>array(
          'method'=>"GET", 
          'header'=>implode('\r\n',$header), 
          'proxy' => 'proxy_address', 
          'request_fulluri' => True 
          ), 
       'timeout' => 60 
); 

這個例子連接超時設置爲60秒

相關問題