本示例使用的读卡器:RFID网络WIFI无线TCP/UDP/HTTP可编程二次开发读卡器POE供电语音-淘宝网 (taobao.com)
<?php
mb_http_output('utf-8');
$port=88;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$bool = socket_bind($socket, "0.0.0.0", $port);
if($bool)echo "Server is listening on port:".$port."\n";
else{echo "Socket bind port:".$port." Error!";exit;
}while($bool){try {$bool = socket_listen($socket);if (!$bool) {echo "LISTEN ERROR:" . socket_strerror(socket_last_error()) . '\n';socket_close($socket);}$new_socket = socket_accept($socket);if (!$new_socket) {$bool=false;socket_close($socket);echo "ACCPET ERROR:" . socket_strerror(socket_last_error()) . '\n';}$string = socket_read($new_socket, 1024);$data = request($string);if(substr($data,0,10)=="Response=1") {$num = socket_write($new_socket, $data);if ($num == 0)echo "WRITE ERROR:" . socket_strerror(socket_last_error()) . "\n";}echo $data . "\n\n";socket_close($new_socket);}catch(Exception $e1) {echo "Server Err:".$e1."\n";}
}/* [读取get或post请求中的包序号、机号、卡号、卡类型、设备序号、读卡状态等,返回相应的字符串驱动读卡器显示文字及播报语音] */
function request($string)
{$info="";$card="";$request="";$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 = $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 = $para[1];break;case "data":$data = $para[1];break;case "dn":$dn = $para[1];break;case "status":$status = $para[1];break;}}if(strlen($info)>0 and strlen($info)>0) {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;}return $ResponseStr;}catch (Exception $e1) {echo "Request ERR:".$e1."\n";}
}//获取中文汉字GB2312编码,显示中文、TTS中文语音必须转换编码
function GetChineseCode($inputstr){if(mb_detect_encoding($inputstr, 'UTF-8', true) === true)$gbkstr=iconv("UTF-8","GB2312",$inputstr); //中文必段强制使用GB2312编码格式else$gbkstr=$inputstr;$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;
}