Javascript 如何使用 PHP 打开 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33080226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:36:58  来源:igfitidea点击:

How to open a URL using PHP?

javascriptphpurlguzzle

提问by Soumi Basu

I want something similar like JavSacript's window.open()method, which I can use to open a particular URL, for example, https://www.google.com/.

我想要类似 JavSacript 的window.open()方法的东西,我可以用它来打开特定的 URL,例如,https://www.google.com/

Is there a way in pure PHPwhere I can do the same thing?

有没有一种纯粹的方法PHP可以让我做同样的事情?

I know we can do that using selenium in python. I think guzzlemight be of some help but I can not find anything useful on the web on that. Thanks in advance.

我知道我们可以在 python 中使用 selenium 来做到这一点。我认为guzzle可能会有所帮助,但我在网上找不到任何有用的信息。提前致谢。

回答by Vivek Srivastava

Although you can not open a new window through PHP directly, you can use:

虽然不能直接通过PHP打开新窗口,但是可以使用:

header("Location: <LOCATION_TO_REDIRECT>");

To redirect the current browser window to the specified URL.

将当前浏览器窗口重定向到指定的 URL。

回答by Amar Singh

If you are just talking about REDIRECTINGthan there are many ways:

如果你只是在谈论 重定向,那么有很多方法:

header("location:ur url");

OR echo"<a href='ur url'></a>";

或者 echo"<a href='ur url'></a>";

OR form submit

或者 form submit

OR using JSin php function :

JS在 php 函数中使用:

<?php
function open_window($url){
   echo '<script>window.open ("'.$url.'", "mywindow","status=0,toolbar=0")</script>;
}';


// test:
open_window('http://www.google.com');
?>

PHP can make any code run... it depends on you how you use PHP.

PHP 可以运行任何代码...这取决于您如何使用 PHP。

回答by Hugo Zink

echoJavaScript somewhere in your page, which contains code to open a new window to the desired location.

echo页面中某处的 JavaScript,其中包含用于在所需位置打开新窗口的代码。

<?php
echo '<script type="text/javascript">
    window.open("http://google.com");
</script>';
?>