Home  >  Article  >  Backend Development  >  PHP code example - general solution for electronic form printing

PHP code example - general solution for electronic form printing

不言
不言Original
2018-04-24 14:49:495296browse

The content of this article is about the general solution for electronic form printing with PHP code examples. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Logistics, express delivery, e-commerce, ERP, warehousing, etc. all involve delivery services and all require the use of electronic bill printing. There are various electronic bill printers, electronic bill printing software, and electronic bill printing tools on the market to meet the needs. Printing needs, but it cannot meet mobile printing well. Here is a general solution for mobile phone electronic form printing : Kuaibao electronic form cloud printing, its advantages: is not limited to printer models and brands, and can print to all major express companies Electronic form, you can print Cainiao electronic form, support batch printing, support remote printing, realize electronic form printer sharing, etc.


##1. Introduction to cloud printing

Kuaibao electronic form cloud printing connects ordinary thermal printers to the cloud with one click, and provides encapsulated printer management, printing task sending, task status updating and other interfaces, helping developers to develop in one day A fully functional and easy-to-use mobile electronic form printing program is developed. It has the following characteristics:

1. Minimalist access: ordinary printers are connected to the mobile shared cloud

2. Extremely fast request: printing can be completed with just one http request

3. Extremely fast customization: the electronic form printing software can be used in half a day


## 2. How to use cloud printing

Kuaibao Cloud Printing includes 6 APIs, which are used in different scenarios and are connected differently according to the process used by the software.


Cloud Printing API ListInterface nameRegister cloud printerDelete cloud printerSend printing taskGet the cloud printer listQuery print task detailsGet cloud printer informationTo use cloud printing, you need to make the following preparations:
Interface description
Change developer account Associate with cloud printer
Unassociate developer account with cloud printer
Send a printing task to the cloud printer
Get the cloud printer information associated with the developer account
Get a certain print task information
Get a certain cloud printer information


Docking preparationPreparationElectronic form printerInstall the cloud printing agent program (download address: http://ckd.so/ydy) Obtained Kuaidi Open Platform developer account information (ID and API Key)

Cloud printing API usage steps, you can choose the API to connect according to the specific business:



3. Cloud printing technology docking (taking PHP as an example)

Interface name: cloud.print.do

Formal address: https://kop.kuaidihelp.com/api

Request method: HTTP POST

Request method encoding format: utf-8

Public request parameters:


Remarks
No limit to brand, no limit to model
Win 7 and above systems, Mac systems are not supported
You can obtain it by registering for Kuaidi Open Platform (http://open.kuaidihelp.com/)
string is the API interface name

Public response parameters


Name Type Is it required Description
##method
##app_idstring is the user ID (assigned when registering for the open platform, view in the console)
signstring is the verification legitimacy signature generated by according to the rules (md5(app_id method ts api_key))
tsstring is the timestamp of the current request
datastring is JSON format request parameter setting
##NameTypeIs it requiredDescriptionint is the response status code. 0-success, non-0-failure

Request parameters


code
##msgstring Yes Return result description
uidstring is the unique business serial number of for this request
datastring is JSON format response data
string is print datastring is the pickup code (optional)string is 1: Bottom order 2: Print 3: Bottom order and print. (Optional, default 3)string is the name of the person is printed. If it is a salesperson, it can include the mobile phone Number (optional) string is Remarks (optional)stringisRecipient address isRecipient city namestring is the recipient’s landline number is the package location information##originrouting outlet companycodestring is the big character and a code is sending information##province##districtstring is the sender’s detailed address is sender’s name## waybill_codewaybill number

Response parameters


Name Type Is it required Description
agent_id string is cloud printer number
action string is the method identifier (print.json.cn)
print_type string is 1: Bottom order 2: Print 3: Bottom order and Print. (Optional, default 3, if set within the task, the setting within the task shall prevail)
batch string is whether it is batch
##print_data
sequence string is the current/total number of , default is 1/1
template_id string is template id view details
cp_code string is express brand
#pickup_code
print_type
user_name
note
##goods_namestring is Item name (optional)
weight string is item weight (optional)
tid string is (it is recommended to use a unique identifier such as the order number. This field will be returned during submission and callback. Application form This field will also be used when the order number)
recipient string is Receive information
##address
##provincestring is Recipient Province
districtstring is Recipient county name
##citystring
detail string is the recipient detailed address
mobile string is recipient mobile phone
name string is recipient name
##phone
##routing_infostring is express routing information
##consolidation string
##namestring is the name of
string is
string is the routing outlet company number
name string is the routing outlet company number name
route_code string is two or three-segment code
##sortation
##namestring is the name of in big characters
##senderstring
##addressstring is the sender address
string is the province of
string is the sender’s county name
city string is the city name of the sender
##detail
##mobilestring is the sender’s mobile phone
# #namestring
## phonestring is the sender’s landline
string is
stringisstatusstring is this request task ID
Name Type Is it required Description
##status
task_id
Supports access to 6 languages ​​including Java, C#, Python, PHP, Node.js, and Curl. The following takes PHP as an example to show the

Send request task code example.

1. Single printing task, request example

$host = "https://kop.kuaidihelp.com/api";
$method = "POST";
$headers = array();
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
$querys = "";
$bodys = [
"app_id"=>'50001',
"method"=>'cloud.print.do',
"sign"=>"bdf3b5f50865ac813cbdfd6c9b572b79",
"ts"=>'1524209949',
"data"=>'{		
			"action":"print.json.cn(菜鸟标准格式JSON)", //
			"agent_id":"目标打印代理", //必填
			"print_type":"打印类型", //1:预览, 2:打印, 3:打印预览. 未设置则默认为3.(任务内设置,则以任务内设置为准)
			"batch":"", //字符串"true"或者bool值 true则为批量,其他值则为非批量 是否为批量
			"print_data": {
				"sequence": "1/1", //可选,打印文档当前序列:当前/总数,默认1/1
				"template_id": "模板id",
				"cp_code": "快递品牌(提供模板id,此字段会被忽略)",//参照“快递品牌列表”,兼容菜鸟和快宝品牌编码
				"pickup_code": "取件码(可选)",
				"print_type": "1:底单 2:打印 3:底单和打印.(可选,默认3)",
				"user_name": "打印人的名称,若是业务员,可包括手机号码(可选)",
				"note": "备注(可选)",
				"goods_name": "物品名(可选)",
				"weight": "物品重量(可选)",
				"tid": "(建议用订单号之类的唯一标识,提交和回调时将返回该字段, 申请单号时也将使用该字段当订单号)",
				"recipient": {
					"address": {
					"city": "宁德市",
					"detail": "哦里咯啦咯啦",
					"district": "福鼎市",
					"province": "福建省"
					},
					"mobile": "13344556677",
					"name": "XXX",
					"phone": ""
				},
				"routing_info": {
					"consolidation": {"name": "温州转福鼎包(集包名)"},
					"origin": {"code": "610025","name": "四川邛崃公司"},
					"route_code": "009 030(二三段码)",
					"sortation": {"name": "福鼎351(大字 一段码)"}
				},
			"sender": {
				"address": {
					"city": "成都市",
					"detail": "兴民街55号",
					"district": "邛崃市",
					"province": "四川省"
				},
				"mobile": "13344556677",
				"name": "XXX",
				"phone": ""
			},
			"waybill_code": "333333333333"
			}
			}'
];
$bodys = http_build_query($bodys);
$url = $host;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
var_dump(curl_exec($curl));

2. Batch printing task, request example

$host = "https://kop.kuaidihelp.com/api";
$method = "POST";
$headers = array();
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
$querys = "";
$bodys = [
"app_id"=>'50001',
"method"=>'cloud.print.do',
"sign"=>"bdf3b5f50865ac813cbdfd6c9b572b79",
"ts"=>'1524209949',
"data"=>'{
    "action":"print.json.cn(菜鸟标准格式JSON)", //
    "agent_id":"目标打印代理", //必填
    "print_type":"打印类型", //1:预览, 2:打印, 3:打印预览. 未设置则默认为3.(任务内设置,则以任务内设置为准)
    "batch":"", //字符串"true"或者bool值 true则为批量,其他值则为非批量 是否为批量
    "print_data":  [
  {
    "cod_amount": 200, //如果是货到付款服务,则使用该字段
    "sequence": "1/1", //可选,打印文档当前序列:当前/总数,默认1/1
    "template_id": "模板id",
    "cp_code": "快递品牌",//参照“快递品牌列表”,兼容菜鸟和快宝品牌编码
    "pickup_code": "取件码(可选)",
    "print_type": "1:底单 2:打印 3:底单和打印.(可选,默认3)",
    "user_name": "打印人的名称,若是业务员,可包括手机号码(可选)",
    "note": "备注(可选)",
    "goods_name": "物品名(可选)",
    "weight": "物品重量(可选)",
    "tid": "(建议用订单号之类的唯一标识,提交和回调时将返回该字段, 申请单号时也将使用该字段当订单号)",
    "recipient": {
      "address": {
        "city": "宁德市",
        "detail": "哦里咯啦咯啦",
        "district": "福鼎市",
        "province": "福建省"
      },
      "mobile": "13344556677",
      "name": "XXX",
      "phone": ""
    },
    "routing_info": {
      "consolidation": {
        "name": "温州转福鼎包(集包名)"
      },
      "origin": {
        "code": "610025",
        "name": "四川邛崃公司"
      },
      "route_code": "009 030(二三段码)",
      "sortation": {
        "name": "福鼎351(大字 一段码)"
      }
    },
    "sender": {
      "address": {
        "city": "成都市",
        "detail": "兴民街55号",
        "district": "邛崃市",
        "province": "四川省"
      },
      "mobile": "13344556677",
      "name": "XXX",
      "phone": ""
    },
    "waybill_code": "333333333333"
  }
]

}'
];
$bodys = http_build_query($bodys);
$url = $host;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
var_dump(curl_exec($curl));

3. Combined with "micro express small mailbox" "(WeChat public account) The order number source configured in it automatically obtains the order number, printing template, and routing information example

$host = "https://kop.kuaidihelp.com/api";
$method = "POST";
$headers = array();
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
$querys = "";
$bodys = [
"app_id"=>'50001',
"method"=>'cloud.print.do',
"sign"=>"bdf3b5f50865ac813cbdfd6c9b572b79",
"ts"=>'1524209949',
"data"=>'{
    "action":"print.json.cn(菜鸟标准格式JSON)", //
    "agent_id":"目标打印代理", //必填
    "print_type":"打印类型", //1:预览, 2:打印, 3:打印预览. 未设置则默认为3.(任务内设置,则以任务内设置为准)
    "batch":"", //字符串"true"或者bool值 true则为批量,其他值则为非批量 是否为批量
    "print_data":  {
  "cod_amount": 200, //如果是货到付款服务,则使用该字段
  "sequence": "1/1", //可选,打印文档当前序列:当前/总数,默认1/1
  "template_id": "模板id",
  "cp_code": "快递品牌(提供模板id,此字段会被忽略)",//参照“快递品牌列表”,兼容菜鸟和快宝品牌编码
  "pickup_code": "取件码(可选)",
  "print_type": "1:底单 2:打印 3:底单和打印.(可选,默认3)",
  "user_name": "打印人的名称,若是业务员,可包括手机号码(可选)",
  "note": "备注(可选)",
  "goods_name": "物品名(可选)",
  "weight": "物品重量(可选)",
  "tid": "(建议用订单号之类的唯一标识,提交和回调时将返回该字段, 申请单号时也将使用该字段当订单号)",
  "sender": {
      "address": {
          "province": "江西省",
          "city": "赣州市",
          "district": "寻乌县",
          "detail": "寻乌县"
      },
      "mobile": "13344556677",
      "phone": "",
      "name": "XXX"
  },
  "recipient": {
      "address": {
          "province": "江西省",
          "city": "宜春市",
          "district": "袁州区",
          "detail": "秀江外滩电13栋"
      },
      "mobile": "13344556677",
      "phone": "",
      "name": "XXX"
  },
  "routing_info": {},
  "waybill_code": "" //留空!留空!留空!
  }


}'
];
$bodys = http_build_query($bodys);
$url = $host;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
var_dump(curl_exec($curl));


-the end-

The above is the detailed content of PHP code example - general solution for electronic form printing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php regular expressionNext article:php regular expression