中远科技电脑自动打印机php接口实例

 
<?php
/**
@ $url, $query=array()参数表,注意不要对参数编码
*/
function http_post($url, $query=array()) {
$ch = curl_init($url) ;
if (is_array($query)) {
foreach ($query as $key => $val) {
$encode_key = $key;
if ($encode_key != $key) {
unset($query[$key]);
}
$query[$encode_key] = $val;
}
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
$response = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno > 0) {
throw new \Exception(curl_error($ch), $errno);
}
if (is_resource($ch)) {
curl_close($ch);
}
return $response;
}

/**$ret 接收返回的参数
** 成功 {"status":"全局信息id","msg":"服务器接收成功"}
** 失败 {"status":0,"msg":"错误提示描述"}
**/
//支持https安全传输
$api = "http://myprinter.waimaipu.cn/2me.php?v=1.0";

$api = "
https://myprinter.waimaipu.cn/2me.php?v=1.0"; //加密传输

$ret = http_post($api,array(
"key"=>'c14f6a384788fc5f970d34987c02a14f',#开发者key
"shopid"=>'6888',#店铺id
"content"=>"我是被打印的数据\r\n我是第二行哦~~",#注意不要编码
"qr"=>'我是二维码吖~'
"copys"=>1, #打印联数
));
$ret = json_decode($ret,1);
if($ret["status"]>0){
#这里是成功发送数据的后期处理,比如把信息id保存起来
# $id = $ret["status"];
exit("<h1>SUCCESS</h1>");
}else{
#失败返回错误提示
header("content-type: text/json;charset=utf-8");
echo $ret["msg"];
}

?>

0 个评论

要回复文章请先登录注册