使用 PHP file_get_contents 发布时如何重定向浏览器?

发布于 2025-01-13 22:54:44 字数 972 浏览 3 评论 0原文

当我收到对我的网络服务器的发布请求时,我添加数据并使用 PHP 和 file_get_contents 发布到另一个网络服务器。问题是浏览器一直显示我的网络服务器的网址,直到用户单击第二个网络服务器页面上的某些内容。

另一方面,如果我接收我的网络服务器收到的帖子,我可以将其作为 get 请求发送到第二个网络服务器,并同时重定向用户的浏览器,以便他们在单击任何内容之前在地址栏中看到第二个网络服务器的 url 。

如何在发帖时使帖子重定向浏览器?

顺便说一句,因为第二个网站使用相对 url,所以我必须在将其发送给用户之前将基本 url 添加到标头标记,以便当他们单击某些内容时,它会解析为正确的 url。

这是我的邮政代码:

  $data = array("key"=>"value",...);
  $options = array(
      "http" => array(
        "header"  => "Content-type: application/x-www-form-urlencoded",
        "method"  => "POST",
        "content" => http_build_query($data))
  );
  $stream  = stream_context_create($options);
  $site = file_get_contents($url, false, $stream);
  $site = str_replace('</head>', '<base href="$baseUrl"></head>', $site);
  echo $site;

这是我的获取代码,工作正常:

  $queryData = http_build_query($data);
  header("location:" . $url . "?" . $queryData);

非常感谢!

When I receive a post request to my webserver, I add to the data and post to another webserver using PHP and file_get_contents. The problem is that the browser keeps showing my webserver's url until the user clicks something on the page from the second webserver.

On the other hand, if I take the post my webserver receives, I can send it to the second webserver as a get request and redirect the user's browser at the same time so that they see the second webserver's url in the addressbar before clicking on anything.

How can I make post redirect the browser while posting?

As a side note, because the second website uses relative urls, I have to add a base url to the header tag before sending it to the user so when they click something, it resolves to the correct url.

Here's my post code:

  $data = array("key"=>"value",...);
  $options = array(
      "http" => array(
        "header"  => "Content-type: application/x-www-form-urlencoded",
        "method"  => "POST",
        "content" => http_build_query($data))
  );
  $stream  = stream_context_create($options);
  $site = file_get_contents($url, false, $stream);
  $site = str_replace('</head>', '<base href="$baseUrl"></head>', $site);
  echo $site;

Here's my get code that works fine:

  $queryData = http_build_query($data);
  header("location:" . $url . "?" . $queryData);

Thank you very much!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文