文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-662971.html
?本示例使用的讀卡器:RFID網(wǎng)絡(luò)WIFI無(wú)線TCP/UDP/HTTP可編程二次開(kāi)發(fā)讀卡器POE供電語(yǔ)音-淘寶網(wǎng) (taobao.com)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-662971.html
<?php
set_time_limit(0);
$BindIp=get_local_ip(); //
$Port=88; //監(jiān)聽(tīng)端口
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(): 監(jiān)聽(tīng)socket的連接請(qǐng)求
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); //解析接收到的數(shù)據(jù),并生成回應(yīng)數(shù)據(jù)
socket_write($client, $response);
//socket_close($client);
}
}
}
catch (Exception $e1) {
echo "Request ERR:".$e1."\n";
}
}
socket_close($socket);
//解析接收到的數(shù)據(jù),并生成回應(yīng)數(shù)據(jù)驅(qū)動(dòng)讀卡器顯示文字、蜂鳴響聲、播報(bào)語(yǔ)音
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來(lái)解板,此處統(tǒng)一替換成字符串處理
$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]); //接收到的數(shù)據(jù)包號(hào),需回應(yīng)該包號(hào)
break;
case "jihao":
$jihao = $para[1]; //設(shè)備機(jī)號(hào)(可自編)
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表示卡離開(kāi)感應(yīng)區(qū)
break;
case "card":
$card = trim($para[1]); //接收到的原始16進(jìn)制卡號(hào),可根據(jù)需要自行轉(zhuǎn)換成其他卡號(hào)
break;
case "data":
$data = $para[1]; //讀取的卡扇區(qū)內(nèi)容
break;
case "dn":
$dn = trim($para[1]); //設(shè)備硬件序列號(hào),出廠時(shí)已固化,全球唯一
break;
case "status":
$status = $para[1]; //讀卡狀態(tài),如密碼認(rèn)證失敗為12
break;
case "heartbeattype":
$heartbeattype = trim($para[1]); //心跳包標(biāo)識(shí)
break;
case "input":
$input = $para[1]; //輸入接口狀態(tài)
break;
case "output":
$output = $para[1]; //輸出接口狀態(tài)
break;
case "time":
$time = $para[1]; //設(shè)備時(shí)鐘
break;
case "rand":
$rand = $para[1]; //隨機(jī)數(shù)
break;
}
}
if (strlen($info)>0 and strlen($dn)==16 and $heartbeattype=="1"){ //接收到設(shè)備的心跳包
//$ResponseStr = "Response=1," . $info . ",,0,0,"; //正式項(xiàng)目可以用這條不顯示文字、不響聲、不播報(bào)語(yǔ)音的指令來(lái)回應(yīng)心跳,此處加入顯示、響聲只是用來(lái)檢測(cè)讀卡器功能
$ResponseStr = "Response=1," . $info . ",".$dn. GetChineseCode("接收到心跳信息 ").",20,1,"; //正式項(xiàng)目可以用這條不顯示文字、不響聲、不播報(bào)語(yǔ)音的指令來(lái)回應(yīng)心跳,此處加入顯示、響聲只是用來(lái)檢測(cè)讀卡器功能
}elseif (strlen($info)>0 and strlen($dn)==16 and (strlen($card)>4)){//接收到有效的刷卡數(shù)據(jù)
if ($pushortake == 0) //播報(bào)的中文語(yǔ)音,[v8]表示語(yǔ)音大小,取值v1 到 v16, TTS中文語(yǔ)音要轉(zhuǎn)換編碼
$ChineseVoice = "[v8]" . GetChineseCode("讀取卡號(hào)[n1]") . $card;
else
$ChineseVoice = "[v8]" . GetChineseCode("卡號(hào)[n1]") . $card . GetChineseCode("離開(kāi)感應(yīng)區(qū)");
//此處可加入業(yè)務(wù)對(duì)數(shù)據(jù)庫(kù)的查詢、刪除、增加、修改等操作,結(jié)果以文字、語(yǔ)音信息回應(yīng)
$DisplayStr = "{" . GetChineseCode("卡號(hào)") . ":}"; //顯示的文字,{}內(nèi)文字可以高亮顯示,中文必須轉(zhuǎn)換編碼,英文字符、數(shù)字等不需要轉(zhuǎn)換
$DisplayStr = $DisplayStr . substr($card . " ", 0, 12);
$DisplayStr = $DisplayStr . substr(date('Y-m-d H:i:s', time()), 2, 17);
//Response=1是固定的回應(yīng)頭信息+接收的包序號(hào)+顯示文字+顯示延時(shí)秒數(shù)+蜂鳴響聲代碼+TTS語(yǔ)音
$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中文語(yǔ)音必須轉(zhuǎn)換編碼
function GetChineseCode($inputstr){
$gbkstr=iconv("UTF-8","GB2312",$inputstr); //中文必段強(qiáng)制使用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;
}
//獲取本機(jī)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/";
//獲取操作系統(tǒng)為win2000/xp、win7的本機(jī)IP真實(shí)地址
$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";
}
}
}
}
//獲取操作系統(tǒng)為linux類型的本機(jī)IP真實(shí)地址
//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]; //如果有多張網(wǎng)塊,可以修改數(shù)組取值來(lái)綁定相應(yīng)的網(wǎng)卡
}else{
return '0.0.0.0';
}
}
?>
到了這里,關(guān)于PHP實(shí)現(xiàn)輕量級(jí)WEB服務(wù)器接收HTTP提交的RFID刷卡信息并回應(yīng)驅(qū)動(dòng)讀卡器顯示播報(bào)語(yǔ)音的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!