Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php发送命令_Php_Url_Header_Command - Fatal编程技术网

通过php发送命令

通过php发送命令,php,url,header,command,Php,Url,Header,Command,我有一个网页index.php,我想从那里通过另一个url发送命令,但不去这个页面。 这是我的代码: <?php $nb = $_GET[‘value’]; if ($nb >= 1000) header('Location: http://ipaddress/.../?command=on'); else if $nb >= 500 && $nb < 1000) header('Location: http://ipaddress/.

我有一个网页index.php,我想从那里通过另一个url发送命令,但不去这个页面。 这是我的代码:

<?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');
?>

如果由服务器进行调用没有问题,请使用例如
file\u get\u contents()
发送命令


如果您需要客户端发出请求,例如,您可以为
img
iframe
元素提供服务,并将目标URL加载到该元素中

通过脚本设置会话是可能的,但您不想包含文件并调用函数或其他什么吗?

如果我理解正确,请改用文件获取内容:

<?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');
?>

是的,您应该看看如何使用

使用
curl
你需要用这个

后示例 例如,要在您将使用的页面上发布命令,请执行以下操作:

<?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);
?>
现在,
$return
包含来自请求URL的内容

文档
curl\u setopt()
中使用的选项是。

是的,您可以使用Ajax。Ajax比cURL或file\u get\u内容更好,因为您将停留在页面上,就在您所在的位置

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调用它:

获取请求的文件\u get\u contents()或获取和发布请求的cURL

是的

$resp=file\u get\u contents('http://ggg.com?foo=bar');

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


您甚至可以使用此方法发布,而无需使用其他库,如cURL。如果您认为此功能可能对您有用,请查看PHP手册中的stream_get_内容。

通过发送
标题('Location:…')
可以隐式重新加载页面。你说“发送命令”是什么意思?从字里行间看,我想你可能在追求某种形式的AJAX功能……我想做与访问url
http://ipaddress/.../?command=on
但我希望我的索引页面保持显示(或最终重新加载),您可以通过单击页面上的按钮或链接来实现这一点,但是不需要重新加载页面吗?实际上,我会在加载页面时自动执行,具体取决于GET的值。我将尝试您的所有解决方案,我会让您知道结果。因为它看起来像是一个(异步)API调用,所以CURL可能是一个不错的选择。您不想包含脚本,只需调用它。这将改变行为:不再从用户的浏览器发出请求。如果命令要求用户登录,则一切将不再有效。问题中需要改变行为!如果请求必须来自用户的浏览器,则我的解决方案不适用。这将改变行为:不再从用户的浏览器发出请求。如果命令要求用户登录,事情将不再有效。同意,但未提及。要求很简单:通过另一个url发送命令,但不转到此页面。如果需要,还可以通过
curl
请求发送身份验证。@Pekka这正是我在前面对您的答案投了赞成票的原因@特雷弗农是的,很可能这是最好的方式。我只是觉得值得一提(为了避免以后再问一个后续问题:)
<?php
$ch = curl_init('http://ipaddress?command=middle');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
?>
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();
    }
}