PHPs会不会遵循301重定向? [英] Will PHPs fopen follow 301 redirects?

查看:156
本文介绍了PHPs会不会遵循301重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一段遗留代码,(ab)通过HTTP使用对资源的fopen()调用:@fopen(' http: //example.com ')。

We have a piece of legacy code that (ab)uses fopen() calls to resources over HTTP: @fopen('http://example.com').

我们希望将example.com移至另一台主机,然后发送301 Permanently Moved。
但是,我们并不完全确定@fopen()是否会遵循这一点。初步测试表明它没有。但也许我会错过一些配置文章。

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.

推荐答案

从版本5.1.0开始, max_redirects选项,这使得fopen HTTP包装器遵循 Location 重定向:

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


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

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

默认为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);
?>

这篇关于PHPs会不会遵循301重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆