带参数的PHP调用
问题描述:
这个问题相当微不足道,但是尽管搜索并尝试了不同的方法,我无法实现它的工作。带参数的PHP调用
,我有以下内容在我的action.php的
<?php
function sendCommand($item, $data)
{
$url = "https://host/rest/items/" . $item;
$options = array(
'http' => array(
'header' => "Content-type: text/plain\r\n",
'method' => 'POST',
'content' => $data //http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
?>
这是我的html页面
<form action="action.php?item=CancelParty&data=ON" method="post" align="center" class="form-inline" role="form">
<button type="submit" class="btn btn-warning btn-fill">Volume down</button>
</form>
的一部分,但我的请求没有被处理。我错过了什么?我通常不会做任何PHP或REST调用,对于webdeveloper这一次一定是很容易的,我认为:)
答
必须实际调用的PHP文件中的函数,最后}
后添加以下行sendCommand($_POST['item'], $_POST['data']);
请记住不要相信来自公共来源的输入。
此外,改变你的表单标签:
<form action="action.php" method="post" align="center" class="form-inline" role="form">
最后,你需要实际提交的数据,我建议增加形式中的以下内容:
<input type="hidden" value="CancelParty" name="item">
<input type="hidden" value="ON" name="data">
如果是整PHP文件,该功能从未被使用过。它已被声明,但不会在任何地方调用。 – junkfoodjunkie