PHP fopen 会遵循 301 重定向吗?

发布于 2024-09-11 02:37:29 字数 254 浏览 7 评论 0原文

我们有一段遗留代码(ab)使用 fopen() 通过 HTTP 调用资源:

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

我们希望将 example.com 移动到另一个主机,然后发送“301 Permanently Moved”,但是,我们不完全确定 @fopen() 是否会遵循这一点。

初步测试表明事实并非如此。但也许我错过了一些配置部分。

We have a piece of legacy code that (ab)uses fopen() 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2024-09-18 02:37:30

从版本 5.1.0 开始,有 max_redirects 选项,这使得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);
?>

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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文