这个 POST 请求是如何工作的?
How does this POST request works?
"Send a message as a POST request to a web service. The address must
start with “http://”, and may optionally include the port number
(default is 80) and the path to a specific web service. The
notification message fills the body of the content part of the POSTed
message, with no key=value form-style formatting – you just read the
input stream directly."
以上摘自Alien UHF RFID F800 manual。上述请求用于将 reader 扫描的 RFID 标签发送到 Web 服务。域名为myrfidtest.com,路径为/insertdb.php。现在 insertdb.php 设置为接受两个参数,例如 id 和 RFID 标签号。所以完整的 URL 是 http://myrfidtest.com/insertdb.php?id=21&rfid=2eda1
。这些数据随后被成功插入到我的数据库中。
因此我了解了如何使用上述 URL 将数据插入云托管数据库。但是,我不明白摘录,“你只是直接读取输入流”是什么意思?
此外,如何更改 insert.php 脚本以接受来自 reader 的标签?
我的 insert.php 脚本:
<?php
class data_new
{
public $conn='';
function __construct($id,$rfid)
{
$this->storeInDB($id,$rfid);
}
function storeInDB($id,$rfid)
{
$conn = new mysqli('localhost','user','password','db');
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "insert into cloud set id='".$id."', rfid='".$rfid."'";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
if($_GET['id'] != '' and $_GET['rfid'] != '')
{
$data_new = new data_new($_GET['id'],$_GET['rfid']);
}
?>
通常当您 post 在 HTTP 请求中形成数据时,您(或您的浏览器)将 posted 数据放入 HTTP 请求的主体中,并将其格式化为类似于查询字符串例如field1=value1&field2=value2
,这样接收请求的服务器就可以区分字段,知道哪个值属于哪个字段。我认为这篇文章是说在这个特定的请求中,请求的整个主体只是一个包含通知数据的字段,没有任何名称=值样式格式 - 因为主体中只有一个参数。
在 PHP 中,posted 数据通常出现在 $_POST
数组中,数组中每个参数都有一个条目(所以你最终会得到 $_POST["field1"]
、$_POST["field2"]
等。但您也可以使用以下方式读取原始输入数据:
$postdata = file_get_contents("php://input");
相反。这在上面提到的情况下很有用,因为数据只是请求正文中的一大串文本,而不是被格式化。
P.S。我无法回答你问题的第二部分 " 我如何更改 insert.php" 因为我不知道你指的是什么脚本,它的作用或者看起来像,或者你在说什么标签。我建议就此提出第二个单独的问题,因为这听起来像是一个不同的问题,并在问题文本中给出一个明确的例子来说明你的意思。
"Send a message as a POST request to a web service. The address must start with “http://”, and may optionally include the port number (default is 80) and the path to a specific web service. The notification message fills the body of the content part of the POSTed message, with no key=value form-style formatting – you just read the input stream directly."
以上摘自Alien UHF RFID F800 manual。上述请求用于将 reader 扫描的 RFID 标签发送到 Web 服务。域名为myrfidtest.com,路径为/insertdb.php。现在 insertdb.php 设置为接受两个参数,例如 id 和 RFID 标签号。所以完整的 URL 是 http://myrfidtest.com/insertdb.php?id=21&rfid=2eda1
。这些数据随后被成功插入到我的数据库中。
因此我了解了如何使用上述 URL 将数据插入云托管数据库。但是,我不明白摘录,“你只是直接读取输入流”是什么意思?
此外,如何更改 insert.php 脚本以接受来自 reader 的标签?
我的 insert.php 脚本:
<?php
class data_new
{
public $conn='';
function __construct($id,$rfid)
{
$this->storeInDB($id,$rfid);
}
function storeInDB($id,$rfid)
{
$conn = new mysqli('localhost','user','password','db');
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "insert into cloud set id='".$id."', rfid='".$rfid."'";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
if($_GET['id'] != '' and $_GET['rfid'] != '')
{
$data_new = new data_new($_GET['id'],$_GET['rfid']);
}
?>
通常当您 post 在 HTTP 请求中形成数据时,您(或您的浏览器)将 posted 数据放入 HTTP 请求的主体中,并将其格式化为类似于查询字符串例如field1=value1&field2=value2
,这样接收请求的服务器就可以区分字段,知道哪个值属于哪个字段。我认为这篇文章是说在这个特定的请求中,请求的整个主体只是一个包含通知数据的字段,没有任何名称=值样式格式 - 因为主体中只有一个参数。
在 PHP 中,posted 数据通常出现在 $_POST
数组中,数组中每个参数都有一个条目(所以你最终会得到 $_POST["field1"]
、$_POST["field2"]
等。但您也可以使用以下方式读取原始输入数据:
$postdata = file_get_contents("php://input");
相反。这在上面提到的情况下很有用,因为数据只是请求正文中的一大串文本,而不是被格式化。
P.S。我无法回答你问题的第二部分 " 我如何更改 insert.php" 因为我不知道你指的是什么脚本,它的作用或者看起来像,或者你在说什么标签。我建议就此提出第二个单独的问题,因为这听起来像是一个不同的问题,并在问题文本中给出一个明确的例子来说明你的意思。