通过php发送命令

发布于 2024-12-02 10:47:48 字数 446 浏览 2 评论 0原文

我有一个网页index.php,我想通过另一个网址发送命令,但不转到此页面。 这是我的代码:

<?php
$nb = $_GET[‘value’];

if ($nb >= 1000)
    header('Location: http://ipaddress/.../?command=on');

else if $nb >= 500 && $nb < 1000)
    header('Location: http://ipaddress/.../?command=middle');

else 
header('Location: http://ipaddress/.../?command=off');
?>

问题是页面由于“标题”而尝试更改,我只想发送命令但保留在我的页面index.php上 有什么办法可以做到这一点吗?

多谢!

I have a webpage index.php from where I would like to send a command through another url but without going to this page.
This is my code:

<?php
$nb = $_GET[‘value’];

if ($nb >= 1000)
    header('Location: http://ipaddress/.../?command=on');

else if $nb >= 500 && $nb < 1000)
    header('Location: http://ipaddress/.../?command=middle');

else 
header('Location: http://ipaddress/.../?command=off');
?>

The problem is that the page tries to change due to the “header”, I just would like to send the command but to stay on my page index.php
Is there any way to do that?

Thanks a lot!

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

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

发布评论

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

评论(8

趴在窗边数星星i 2024-12-09 10:47:48

使用 curl

您需要使用 curl (手册页)为了这。

POST 示例

例如,将命令发布到您将使用的页面上:

<?php
$ch = curl_init('http://ipaddress');

$data = array('command' => 'middle');

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>

GET 示例

<?php
$ch = curl_init('http://ipaddress?command=middle');   
curl_exec($ch);
?>

防止请求的 URL 内容输出到屏幕

您需要查看 curl_setopt() 的 CURLOPT_RETURNTRANSFER 选项。例如:

<?php
$ch = curl_init('http://ipaddress?command=middle');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
?>

中的内容

现在 $return 包含请求的 URL文档

curl_setopt() 中使用的选项为 记录在此处

Use curl

You need to use curl (man page) for this.

POST Example

For example to post a command on to the page you would use:

<?php
$ch = curl_init('http://ipaddress');

$data = array('command' => 'middle');

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>

GET example

<?php
$ch = curl_init('http://ipaddress?command=middle');   
curl_exec($ch);
?>

Preventing the requested URL's content from being output to screen

You need to look at the CURLOPT_RETURNTRANSFER option of curl_setopt(). For example:

<?php
$ch = curl_init('http://ipaddress?command=middle');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
?>

Now $return contains the content fromthe requested URL

Documentation

The options used in curl_setopt() are documented here.

趁微风不噪 2024-12-09 10:47:48

如果可以由服务器进行调用,请使用例如file_get_contents() 发送命令。

如果您需要客户端发出请求,您可以提供一个 imgiframe 元素并将目标 URL 加载到其中。

If it's fine that it's the server doing the call, use e.g. file_get_contents() to send the command.

If you need the client to make the request, you could e.g. serve an img or iframe element and load the target URL into that.

烟织青萝梦 2024-12-09 10:47:48

如果我理解正确,请改用 file_get_contents :

<?php
$nb = $_GET[‘value’];

if ($nb >= 1000)
    file_get_contents('http://ipaddress/.../?command=on');

else if $nb >= 500 && $nb < 1000)
    file_get_contents('http://ipaddress/.../?command=middle');

else 
    file_get_contents('http://ipaddress/.../?command=off');
?>

If I understand correctly, use file_get_contents instead:

<?php
$nb = $_GET[‘value’];

if ($nb >= 1000)
    file_get_contents('http://ipaddress/.../?command=on');

else if $nb >= 500 && $nb < 1000)
    file_get_contents('http://ipaddress/.../?command=middle');

else 
    file_get_contents('http://ipaddress/.../?command=off');
?>
恬淡成诗 2024-12-09 10:47:48

是的,您可以使用 Ajax 来实现这一点。 Ajax 比 cURL 或 file_get_contents 更好,因为您将停留在页面上,就在您所在的位置。

function SendData(strData)
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            // The page has been requested and your code has been run
        }
        xmlhttp.open("GET","http://ipaddress/.../?command=" + strData, true);
        xmlhttp.send();
    }
}

用 HTML 调用它:
发送ON

Yes, you could use Ajax for that. Ajax is better than cURL or file_get_contents since you will stay on your page, exactly where you are.

function SendData(strData)
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            // The page has been requested and your code has been run
        }
        xmlhttp.open("GET","http://ipaddress/.../?command=" + strData, true);
        xmlhttp.send();
    }
}

Call it with HTML:
<a href='#' onClick='SendData("on");'>Send ON</a>

赢得她心 2024-12-09 10:47:48

可以通过从脚本中设置 cURL 会话来实现 - 但不要这样做您想包含该文件并调用函数或其他什么?

It's possible, by setting up a cURL session from your script - but don't you rather want to include the file and call a function or something?

享受孤独 2024-12-09 10:47:48

是的,您应该考虑使用 cURL

Yes, you should look at using cURL.

泛泛之交 2024-12-09 10:47:48

file_get_contents() 用于 GET 请求,或 cURL 用于 GET 和 POST 请求

file_get_contents() for GET request or cURL for GET and POST requests

烟若柳尘 2024-12-09 10:47:48

是的。

$resp = file_get_contents('http://ggg.com?foo=bar');

查看 file_get_contents 和 fopen 的 PHP 手册,它们具有用于获取 http 内容的包装器,在大多数版本中默认启用。

您甚至可以使用此方法进行 POST,而无需使用 cURL 等其他库。如果您认为此功能可能对您有用,请查看 PHP 手册中的stream_get_contents。

Yup.

$resp = file_get_contents('http://ggg.com?foo=bar');

Check out the PHP manual for file_get_contents and fopen which have wrappers for fetching http content, enabled by default in most builds.

You can even POST using this method without having to use additional libraries like cURL. If you think this functionality might be useful to you, have a look at stream_get_contents in the PHP manual.

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