芯烨小票打印机PHP打印代码,案例中打印机为58mm打印机
58mm的机器,一行打印16个汉字,32个字母
80mm的机器,一行打印24个汉字,48个字母
本次添加了补打空格的代码,根据商品名称计算后面需要增加多少空格能对齐价格和份数。
所有代码如下:
1 <?php 2 3 namespace app\gctest\controller; 4 class Index 5 { 6 public function index(){ 7 header("Content-type:text/html;charset=utf-8");//必须 官方要求 8 header("Access-Control-Allow-Origin: *");//必须 否则可能报跨域错误 9 $user = '[email protected]';//芯烨云平台注册用户名(开发者 ID) 10 $timestamp = time();//当前UNIX时间戳,10位,精确到秒 11 $user_key = '0f04568213985625f45a2';//用户开发者密钥 12 $sign = sha1($user.$user_key.$timestamp);//对参数 user + user_key + timestamp 拼接后(+号表示连接符)进行SHA1加密得到签名,值为40位小写字符串 13 $sn = '02B58MLF221254B';//打印机编号 14 $arr=array(array("牛肉面","20元","2份"),array("红焖猪手砂锅饭","25元","3份"),array("可乐","9元","2份")); 15 $content0=''; 16 foreach($arr as $k=>$v){ 17 $space =$this->addspace($v[0]); 18 //var_dump($v[0]); 19 $content0.='<L><HB>'.$v[0].$space.$v[1].' '.$v[2].'<BR>'; 20 } 21 $content = 22 '<CB>**店铺名称** 23 <L><N>-------------------------------- 24 <CB>--在线支付-- 25 <L><N>下单时间:2023年5月9日22时32分 26 订单编号:'.rand(100,1000).' 27 **************商品************** 28 <C>---1号窗口---</C><BR>'. 29 $content0. 30 '牛肉 x1 8 31 -------------------------------- 32 配送费:¥4 33 -------------------------------- 34 <N>小计:¥31 35 <L><N>******************************** 36 <N>订单总价:¥35 37 <HB>香洲花园 5栋6单元1404号 38 张先生:18966717535 39 订单备注:[用餐人数]1人; 40 少放辣椒 41 <C><HB>**#8 完** 42 <BR><BR>';//打印内容 43 $data = json_encode(array("user"=>$user,"timestamp"=>$timestamp,"sign"=>$sign,"sn"=>$sn,"content"=>$content)); 44 $url = "https://open.xpyun.net/api/openapi/xprinter/print"; 45 $result = $this->curl_post($url,$data); 46 var_dump($result); 47 } 48 49 function curl_post($url,$data){ 50 $header = array('Content-Type: application/json; charset=utf-8'); 51 $ch = curl_init(); 52 curl_setopt($ch, CURLOPT_URL, $url); 53 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 54 curl_setopt($ch, CURLOPT_POST, 1); 55 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 56 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 57 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 58 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 59 curl_setopt($ch, CURLOPT_HEADER, 0); 60 curl_setopt($ch, CURLOPT_TIMEOUT, 10); 61 $output = curl_exec($ch); 62 curl_close($ch); 63 return $output; 64 } 65 66 function addspace($pname){ 67 $strlen0 = mb_strlen($pname); 68 $strlen = 10-$strlen0; 69 for($i=0;$i<$strlen;$i++){ 70 $sp.=" "; 71 } 72 return $sp; 73 } 74 }
标签:打印机,ch,setopt,芯烨,小票,打印,curl,PHP,CURLOPT From: https://www.cnblogs.com/jeib/p/17387628.html