从飞鹅打印机官网http://www.feieyun.cn/api.php 下载对应的文档。这里以php为例
下载后,目录如下:
这是官方封装好的打印机类,没有特殊需求,不需要去改动里面的方法。
HttpClient.class.php
在这个文件里是写调用打印机的方法,拼接需要打印的字符内容,里面的方法,根据需要自己做相应的改动:
<?php header("Content-type: text/html; charset=utf-8"); include ‘HttpClient.class.php‘; define(‘USER‘, ‘xxxxxxxxxxxxx‘); //*用户填写*:飞鹅云后台注册账号 define(‘UKEY‘, ‘xxxxxxxxxxxxx‘); //*用户填写*: 飞鹅云注册账号后生成的UKEY //API URL define(‘IP‘,‘api.feieyun.cn‘); //接口IP或域名 define(‘PORT‘,80); //接口IP端口 define(‘HOSTNAME‘,‘/Api/Open/‘); //接口路径 define(‘STIME‘, time()); //公共参数,请求时间 define(‘SIG‘, sha1(USER.UKEY.STIME)); //公共参数,请求公钥 //==================方法1.打印订单================== //***接口返回值说明*** //正确例子:{"msg":"ok","ret":0,"data":"316500004_20160823165104_1853029628","serverExecutedTime":6} //错误:{"msg":"错误信息.","ret":非零错误码,"data":null,"serverExecutedTime":5} //标签说明: //"<BR>"为换行符 //"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果) //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片) //"<PLUGIN>"为钱箱或者外置音响指令 //"<CB></CB>"为居中放大 //"<B></B>"为放大一倍 //"<C></C>"为居中 //"<L></L>"为字体变高一倍 //"<W></W>"为字体变宽一倍 //"<QR></QR>"为二维码 //"<RIGHT></RIGHT>"为右对齐 //拼凑订单内容时可参考如下格式 //根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式 $orderInfo = ‘<CB>测试打印</CB><BR>‘; $orderInfo .= ‘名称 单价 数量 金额<BR>‘; $orderInfo .= ‘--------------------------------<BR>‘; $orderInfo .= ‘饭 10.0 10 10.0<BR>‘; $orderInfo .= ‘炒饭 10.0 10 10.0<BR>‘; $orderInfo .= ‘蛋炒饭 10.0 100 100.0<BR>‘; $orderInfo .= ‘鸡蛋炒饭 100.0 100 100.0<BR>‘; $orderInfo .= ‘西红柿炒饭 1000.0 1 100.0<BR>‘; $orderInfo .= ‘西红柿蛋炒饭 100.0 100 100.0<BR>‘; $orderInfo .= ‘西红柿鸡蛋炒饭 15.0 1 15.0<BR>‘; $orderInfo .= ‘备注:加辣<BR>‘; $orderInfo .= ‘--------------------------------<BR>‘; $orderInfo .= ‘合计:xx.0元<BR>‘; $orderInfo .= ‘送货地点:广州市南沙区xx路xx号<BR>‘; $orderInfo .= ‘联系电话:13888888888888<BR>‘; $orderInfo .= ‘订餐时间:2014-08-08 08:08:08<BR>‘; $orderInfo .= ‘<QR>http://www.dzist.com</QR>‘;//把二维码字符串用标签套上即可自动生成二维码 //打开注释可测试 //wp_print("打印机编号",$orderInfo,1); //===========方法2.查询某订单是否打印成功============= //***接口返回值说明*** //正确例子: //已打印:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":6} //未打印:{"msg":"ok","ret":0,"data":false,"serverExecutedTime":6} //打开注释可测试 //$orderindex = "xxxxxxxxxxxxxx";//订单索引,从方法1返回值中获取 //queryOrderState($orderindex); //===========方法3.查询指定打印机某天的订单详情============ //***接口返回值说明*** //正确例子:{"msg":"ok","ret":0,"data":{"print":6,"waiting":1},"serverExecutedTime":9} //打开注释可测试 //$sn = "xxxxxxxxx";//打印机编号 //$date = "2016-08-27";//注意时间格式为"yyyy-MM-dd",如2016-08-27 //queryOrderInfoByDate($sn,$date); //===========方法4.查询打印机的状态========================== //***接口返回值说明*** //正确例子: //{"msg":"ok","ret":0,"data":"离线","serverExecutedTime":9} //{"msg":"ok","ret":0,"data":"在线,工作状态正常","serverExecutedTime":9} //{"msg":"ok","ret":0,"data":"在线,工作状态不正常","serverExecutedTime":9} //打开注释可测试 //queryPrinterStatus("打印机编号"); /* * 方法1 拼凑订单内容时可参考如下格式 根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式 */ function wp_print($printer_sn,$orderInfo,$times){ $content = array( ‘user‘=>USER, ‘stime‘=>STIME, ‘sig‘=>SIG, ‘apiname‘=>‘Open_printMsg‘, ‘sn‘=>$printer_sn, ‘content‘=>$orderInfo, ‘times‘=>$times//打印次数 ); $client = new HttpClient(IP,PORT); if(!$client->post(HOSTNAME,$content)){ echo ‘error‘; } else{ echo $client->getContent(); } } /* * 方法2 根据订单索引,去查询订单是否打印成功,订单索引由方法1返回 */ function queryOrderState($index){ $msgInfo = array( ‘user‘=>USER, ‘stime‘=>STIME, ‘sig‘=>SIG, ‘apiname‘=>‘Open_queryOrderState‘, ‘orderid‘=>$index ); $client = new HttpClient(IP,PORT); if(!$client->post(HOSTNAME,$msgInfo)){ echo ‘error‘; } else{ $result = $client->getContent(); echo $result; } } /* * 方法3 查询指定打印机某天的订单详情 */ function queryOrderInfoByDate($printer_sn,$date){ $msgInfo = array( ‘user‘=>USER, ‘stime‘=>STIME, ‘sig‘=>SIG, ‘apiname‘=>‘Open_queryOrderInfoByDate‘, ‘sn‘=>$printer_sn, ‘date‘=>$date ); $client = new HttpClient(IP,PORT); if(!$client->post(HOSTNAME,$msgInfo)){ echo ‘error‘; } else{ $result = $client->getContent(); echo $result; } } /* * 方法4 查询打印机的状态 */ function queryPrinterStatus($printer_sn){ $msgInfo = array( ‘user‘=>USER, ‘stime‘=>STIME, ‘sig‘=>SIG, ‘debug‘=>‘nojson‘, ‘apiname‘=>‘Open_queryPrinterStatus‘, ‘sn‘=>$printer_sn ); $client = new HttpClient(IP,PORT); if(!$client->post(HOSTNAME,$msgInfo)){ echo ‘error‘; } else{ $result = $client->getContent(); echo $result; } } ?>
上述方法里的常量在注册后可获得
define(‘USER‘, ‘xxxxxxxxxxxxx‘); //*用户填写*:飞鹅云后台注册账号define(‘UKEY‘, ‘xxxxxxxxxxxxx‘); //*用户填写*: 飞鹅云注册账号后生成的UKEY如下图:
本文为个人原创,望能便利自己,有益他人。
时间: 2024-10-12 14:02:26