2013-04-26 31 views
0

我試圖讓一個頁面的內容是這樣的:的http_access返回(也simple_html_dom)

<?php 
include_once 'simple_html_dom.php'; 
     $opts = array('http' => 
      array(
        'method' => 'GET', 
        'timeout' => 10 
      ) 
    ); 
    $domain = "http://www.esperandoaramon.com"; 
    //$domain = "http://www.google.com"; 
    $context = stream_context_create($opts); 
    $input = @file_get_contents($domain,false,$context) or die("Could not access file: $domain"); 
    echo($input); 
?> 

我能得到www.google.com內容這樣,不幸的是,其他域只給我這個通知:

Notice: 
Text: Undefined index: HTTP_ACCEPT 
File: /home/trdeport/public_html/esperandoaramon/_visit.php 
Line: 4 

這HTTP_ACCEPT正在殺我......該頁面在瀏覽器上完美運行。有什麼解決方法嗎?

回答

1

看來問題出在網站的另一端,而不是你的腳本。我懷疑,其他站點預計的Accept頭,當它沒有一個,它失敗(它與你的瀏覽器,因爲瀏覽器總是發出這個頭。)嘗試在你流上下文選項設置它:

$opts = array(
    'http' => array(
     'method' => 'GET', 
     'timeout' => 10, 
     'header' => "Accept: text/html\r\n" 
    ) 
); 
+0

非常感謝pilsetnieks!就像一個魅力^^ – vivoconunxino 2013-04-26 20:41:16

+0

我終於不得不添加下面的解釋:'header'=>「Accept:text/html \ r \ n」。 「User-Agent:Mozilla/5.0(X11; Linux x86_64; rv:12.0)Gecko/20100101 Firefox/12.0」 – vivoconunxino 2013-04-26 20:42:14