2010-07-20 71 views
6

我們有一段遺留代碼(ab)使用fopen()調用HTTP上的資源:@​​fopen('http://example.com')。PHPs會打開301重定向嗎?

我們想將example.com移動到另一個主機,然後發送301永久移動。 但是,我們並不完全確定@fopen()是否會遵循這一點。初步測試表明,它沒有。但也許我想念一些配置。

+1

我預見到了將來的重構! – Stephen 2010-07-20 12:00:53

+0

當然。這個重定向是這個重構的第一步,實際上:) – berkes 2010-07-20 12:14:59

回答

3

自5.1.0版本,還有的max_redirects option,這使得則fopen HTTP包裝遵循Location重定向:

重定向的最大數量遵循。值1或更小意味着不遵循重定向。

默認爲20

您可能需要顯式設置,如果你的配置禁用此。從文檔修改的一個示例:

<?php 

$url = 'http://www.example.com/'; 

$opts = array(
     'http' => array('method' => 'GET', 
         'max_redirects' => '20') 
     ); 

$context = stream_context_create($opts); 
$stream = fopen($url, 'r', false, $context); 

// header information as well as meta data 
// about the stream 
var_dump(stream_get_meta_data($stream)); 

// actual data at $url 
var_dump(stream_get_contents($stream)); 
fclose($stream); 
?>