飛鵝WiFi打印機配置,php調用接口


 

從飛鵝打印機官網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
如下圖:

 



本文為個人原創,望能便利自己,有益他人。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM