开发者

Will PHPs fopen follow 301 redirects?

We have a piece of legacy code that (ab)uses f开发者_高级运维open() calls to resources over HTTP:

@fopen('http://example.com')

We want to move example.com to another host and then send "301 Permanently Moved", however, we are not entirely sure if @fopen() will follow this.

Initial tests show me that it does not. But maybe I miss some configuration piece.


Since version 5.1.0, there's the max_redirects option, which makes the fopen HTTP wrapper follow the Location redirect:

The max number of redirects to follow. Value 1 or less means that no redirects are followed.

Defaults to 20.

You might want to set it explicitly, in case your config disables this. An example modified from the docs:

<?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);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜