2010-05-31 64 views
4

我需要獲取網頁的內容,因爲它沒有啓用,所以我不能使用Curl。我嘗試了下面的代碼但它不工作。如何在沒有CURL的情況下獲取網頁的內容?

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
       "Cookie: foo=bar\r\n" 
) 
); 

$context = stream_context_create($opts); 

$fp = fopen($_GET['url'], 'r', false, $context); 
if($fp) 
fpassthru($fp); 
fclose($fp); 
exit; 

的代碼產生一個錯誤

Warning: fopen(http://www.google.com/search?&q=site:www.myspace.com+-intitle:MySpaceTV+%22Todd Terje%22) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

回答

4

你是否注意到Todd和Terje之間的URL有一個ACTUAL空間?這可能會導致你的問題,因爲瀏覽器通常編碼爲+%20

+0

我可以說什麼「你規則」:P鷹眼;)。是的,問題是! – Arshdeep 2010-05-31 21:12:55

+7

要將此答案標記爲「您規則」,請單擊左側的綠色勾號標記。 – Douglas 2010-05-31 21:43:15

3

您可以使用該file_get_contents功能:

$content = file_get_contents('url/filepath here'); 
echo $content; 

注:如果你想從安全協議如閱讀https,請確保您有openssl ex緊張局勢從php.ini開啓。

更新:

從你說的話,我懷疑你有allow_url_fopen設置從php.ini文件被關閉,你需要打開上要能夠從URL中讀取。

更新2:

看起來你沒有指定正確的網址,我只是檢查,例如,如果你只是把www.google.com,它工作正常:

$url = 'http://www.google.com'; 
$content = file_get_contents($url); 
echo $content; 
+0

是的我已經嘗試過了,ERROR「警告:的file_get_contents()[function.file-獲取內容]:未能打開流:HTTP請求失敗!HTTP/1.0 400錯誤請求「 – Arshdeep 2010-05-31 20:45:58

+0

@Arsheep:請參閱我更新的答案。 – Sarfraz 2010-05-31 20:47:39

+0

好吧,我看到了,不是一個安全的網址「並於allow_url_fopen =在」我檢查 – Arshdeep 2010-05-31 20:51:48

1

你可以實際上在file_get_contents中指定了一個URL而不是文件名。

+0

我知道兄弟我已經試過了,錯誤「Warning:file_get_contents()[function.file-get-contents]:未能打開流:HTTP請求失敗!HTTP/1.0 400錯誤請求」 – Arshdeep 2010-05-31 20:48:51

0

使用WireShark等嗅探器來獲取實際瀏覽器請求的內容。然後複製它並逐個刪除,很快你會得到最少的標題。

4

你可以使用老式的代碼,如:

$CRLF = "\r\n"; 
$hostname = "www.something.com"; 

$headers[] = "GET ".$_GET['url']." HTTP/1.1"; 
$headers[] = "Host: ".$hostname; 
$headers[] = "Accept-language: en"; 
$headers[] = "Cookie: foo=bar"; 
$headers[] = ""; 

$remote = fsockopen($hostname, 80, $errno, $errstr, 5); 
// a pinch of error handling here 

fwrite($remote, implode($CRLF, $headers).$CRLF); 

$response = ''; 

while (! feof($remote)) 
{ 
    // Get 1K from buffer 
    $response .= fread($remote, 1024); 
} 

fclose($remote); 

更新:這個解決方案的好處是,它不依賴於fopen封裝。

-1
php file_get_contents() function 

nadeausoftware.com/articles/2007/07/php_tip_how_get_web_page_using_fopen_wrappers

/** 
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an 
* array containing the HTTP server response header fields and content. 
*/ 
function get_web_page($url) 
{ 
    $options = array(
     CURLOPT_RETURNTRANSFER => true,  // return web page 
     CURLOPT_HEADER   => false, // don't return headers 
     CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
     CURLOPT_ENCODING  => "",  // handle all encodings 
     CURLOPT_USERAGENT  => "spider", // who am i 
     CURLOPT_AUTOREFERER => true,  // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
     CURLOPT_TIMEOUT  => 120,  // timeout on response 
     CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
    ); 

    $ch  = curl_init($url); 
    curl_setopt_array($ch, $options); 
    $content = curl_exec($ch); 
    $err  = curl_errno($ch); 
    $errmsg = curl_error($ch); 
    $header = curl_getinfo($ch); 
    curl_close($ch); 

    $header['errno'] = $err; 
    $header['errmsg'] = $errmsg; 
    $header['content'] = $content; 
    return $header; 
} 

THX:http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

+0

哇,我想你忘記了「如何在沒有CURL的情況下獲取網頁內容?」 – Arshdeep 2010-05-31 21:05:55

+0

我因爲OP不想使用CURL而降低了投票。然而,如果你刪除curl位幷包含一些不使用CURL的示例代碼,我可能會贊成你。 – 2010-05-31 21:41:07

相關問題