文章来源:https://www.toymoban.com/news/detail-662971.html
本示例使用的读卡器:RFID网络WIFI无线TCP/UDP/HTTP可编程二次开发读卡器POE供电语音-淘宝网 (taobao.com)文章来源地址https://www.toymoban.com/news/detail-662971.html
<?php
set_time_limit(0);
$BindIp=get_local_ip(); //
$Port=88; //监听端口
if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE){
echo 'Failed to initialize the socket:' . socket_strerror(socket_last_error($socket));
exit;
}
// socket_bind(): 将socket资源绑定到指定地址
if(!socket_bind($socket, $BindIp, $Port)){
echo 'Failed to bind the port:' . socket_strerror(socket_last_error($socket));
exit;
}
// socket_listen(): 监听socket的连接请求
if(!socket_listen($socket)){
echo 'Listening port:' . socket_strerror(socket_last_error($socket));
exit;
}else{echo "HTTP service has been started and is listening on the port:".$BindIp.":".$Port."\n\n";}
while(1){
try {
if (($client = socket_accept($socket)) !== FALSE) {
$content = socket_read($client, 20480);
$content = trim($content);
if ($content) {
$response = AnalyzeData($content); //解析接收到的数据,并生成回应数据
socket_write($client, $response);
//socket_close($client);
}
}
}
catch (Exception $e1) {
echo "Request ERR:".$e1."\n";
}
}
socket_close($socket);
//解析接收到的数据,并生成回应数据驱动读卡器显示文字、蜂鸣响声、播报语音
function AnalyzeData($string)
{
$info="";
$card="";
$request="";
$heartbeattype="";
$ResponseStr="";
try {
echo $string . "\n";
$requestmode = substr($string, 0, 4);
if ($requestmode == "GET ") {
$begin = stripos($string, "?") + 1;
$end = stripos($string, "HTTP/1.1");
$request = substr($string, $begin, $end - $begin);
} elseif ($requestmode == "POST") {
$pattern = "/\s+/";
$FieldsList = preg_split($pattern, $string);
$conuts = count($FieldsList);
$isjson = stripos($string, "application/json");
$request = $FieldsList[$conuts - 1];
if ($isjson > 0) { //可以直接使用JSON来解板,此处统一替换成字符串处理
$request = str_replace("{", "", $request);
$request = str_replace("}", "", $request);
$request = str_replace("\"", "", $request);
$request = str_replace(":", "=", $request);
$request = str_replace(",", "&", $request);
}
}
$Fields = explode("&", $request);
$conuts = count($Fields);
for ($p = 0; $p < $conuts; $p++) {
$para = explode("=", $Fields[$p]);
switch ($para[0]) {
case "info":
$info = trim($para[1]); //接收到的数据包号,需回应该包号
break;
case "jihao":
$jihao = $para[1]; //设备机号(可自编)
break;
case "cardtype":
$cardtype = $para[1];
$typenum = hexdec($cardtype) % 16; //typenum=1 ID卡,2 HID卡,3 T5557卡,4 EM4305卡,5 IC卡,6 二代身份证,7 是15693卡,IClass"
$pushortake = intval(hexdec($cardtype) / 128); //pushortake=0 表示读卡,>0表示卡离开感应区
break;
case "card":
$card = trim($para[1]); //接收到的原始16进制卡号,可根据需要自行转换成其他卡号
break;
case "data":
$data = $para[1]; //读取的卡扇区内容
break;
case "dn":
$dn = trim($para[1]); //设备硬件序列号,出厂时已固化,全球唯一
break;
case "status":
$status = $para[1]; //读卡状态,如密码认证失败为12
break;
case "heartbeattype":
$heartbeattype = trim($para[1]); //心跳包标识
break;
case "input":
$input = $para[1]; //输入接口状态
break;
case "output":
$output = $para[1]; //输出接口状态
break;
case "time":
$time = $para[1]; //设备时钟
break;
case "rand":
$rand = $para[1]; //随机数
break;
}
}
if (strlen($info)>0 and strlen($dn)==16 and $heartbeattype=="1"){ //接收到设备的心跳包
//$ResponseStr = "Response=1," . $info . ",,0,0,"; //正式项目可以用这条不显示文字、不响声、不播报语音的指令来回应心跳,此处加入显示、响声只是用来检测读卡器功能
$ResponseStr = "Response=1," . $info . ",".$dn. GetChineseCode("接收到心跳信息 ").",20,1,"; //正式项目可以用这条不显示文字、不响声、不播报语音的指令来回应心跳,此处加入显示、响声只是用来检测读卡器功能
}elseif (strlen($info)>0 and strlen($dn)==16 and (strlen($card)>4)){//接收到有效的刷卡数据
if ($pushortake == 0) //播报的中文语音,[v8]表示语音大小,取值v1 到 v16, TTS中文语音要转换编码
$ChineseVoice = "[v8]" . GetChineseCode("读取卡号[n1]") . $card;
else
$ChineseVoice = "[v8]" . GetChineseCode("卡号[n1]") . $card . GetChineseCode("离开感应区");
//此处可加入业务对数据库的查询、删除、增加、修改等操作,结果以文字、语音信息回应
$DisplayStr = "{" . GetChineseCode("卡号") . ":}"; //显示的文字,{}内文字可以高亮显示,中文必须转换编码,英文字符、数字等不需要转换
$DisplayStr = $DisplayStr . substr($card . " ", 0, 12);
$DisplayStr = $DisplayStr . substr(date('Y-m-d H:i:s', time()), 2, 17);
//Response=1是固定的回应头信息+接收的包序号+显示文字+显示延时秒数+蜂鸣响声代码+TTS语音
$ResponseStr = "Response=1," . $info . "," . $DisplayStr . ",20,1," . $ChineseVoice;
}else{
$ResponseStr = " "; //其他未知的信息
}
echo $ResponseStr . "\n\n";
return $ResponseStr;
}
catch (Exception $e1) {
echo "Request ERR:".$e1."\n";
}
}
//获取中文汉字GB2312编码,显示中文、TTS中文语音必须转换编码
function GetChineseCode($inputstr){
$gbkstr=iconv("UTF-8","GB2312",$inputstr); //中文必段强制使用GB2312编码格式
$strlens=strlen($gbkstr);
$hexcode="";
for($i=0;$i<$strlens;$i++){
if($i % 2 ==0) {
$hexcode = $hexcode . "\\x";
}
$byte = ord($gbkstr[$i]);
$b=substr('00'.dechex($byte ),-2);
$hexcode=$hexcode.$b;
}
return $hexcode;
}
//获取本机IP地址------------------------------------------------------------------------------------------------------------------------
function get_local_ip()
{
$preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
//获取操作系统为win2000/xp、win7的本机IP真实地址
$Netdriv=array();
exec("ipconfig", $out, $stats);
if (!empty($out)) {
foreach ($out AS $row) {
if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
$tmpIp = explode(":", $row);
if (preg_match($preg, trim($tmpIp[1]))) {
$Netdriv[]=trim($tmpIp[1]);
echo trim($tmpIp[1])."\n";
}
}
}
}
//获取操作系统为linux类型的本机IP真实地址
//exec("ifconfig", $out, $stats);
//if (!empty($out)) {
// if (isset($out[1]) && strstr($out[1], 'addr:')) {
// $tmpArray = explode(":", $out[1]);
// $tmpIp = explode("", $tmpArray[1]);
// if (preg_match($preg, trim($tmpIp[0]))) {
// return trim($tmpIp[0]);
// }
// }
//}
if (!empty($Netdriv)) {
return $Netdriv[0]; //如果有多张网块,可以修改数组取值来绑定相应的网卡
}else{
return '0.0.0.0';
}
}
?>
到了这里,关于PHP实现轻量级WEB服务器接收HTTP提交的RFID刷卡信息并回应驱动读卡器显示播报语音的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!