2016-08-08 14 views
0

工作我使用抓取網站下面的代碼的file_get_contents不是從我的服務器

require 'simple_html_dom.php'; 

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n")); 
$context = stream_context_create($opts); 
$header = file_get_contents('http://www.cinnetwork.org',false,$context); 
print_r($header); 

此代碼正確phpfiddle。但是當我從我的服務器使用它時,它說

Warning: file_get_contents(http://www.cinnetwork.org): failed to open stream: Connection timed out

我也嘗試使用捲曲。它也顯示了類似的錯誤。

+0

爲config'allow_url_fopen'打開? – Ghost

+0

allow_url_fopen已打開 –

+0

該代碼適用於其他網站。 –

回答

0
$url= 'https://example.com'; 

$arrContextOptions=array(
    "ssl"=>array(
     "verify_peer"=>false, 
     "verify_peer_name"=>false, 
    ), 
); 

$response = file_get_contents($url, false, stream_context_create($arrContextOptions)); 
+0

在phpfiddle中工作,但顯示同樣的錯誤,而從我的服務器做到無法打開流:連接超時。但相同的代碼適用於其他網站,雖然 –

+0

雖然此代碼片段可以解決這個問題,[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)真的有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要使用解釋性註釋來擠佔代碼,因爲這會降低代碼和解釋的可讀性! – FrankerZ

0

一個建議,你可以使用捲曲,而不是..

+0

http://simplehtmldom.sourceforge.net/manual_faq.htm –

+0

我aso嘗試捲曲,但給了類似的錯誤 –

+0

嘗試不同的網址..以確保此網站是否阻止了一些東西..嘗試獲取數據,如雅虎和谷歌捲曲然後用文件獲取內容。 –

相關問題