使用 PHP file_get_contents 发布时如何重定向浏览器?
当我收到对我的网络服务器的发布请求时,我添加数据并使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论