printer_setting, true); if ($printer_setting) { $this->app_id = $printer_setting['app_id']; $this->app_secret = $printer_setting['app_secret']; } } } protected function request($url, $data) { $data = json_encode($data); $uid = uniqid('rxid'); $time = time(); $md5 = md5($uid . $this->app_id . $time . $this->app_secret . $data); $header = [ "Content-Type:application/json", "appid:" . $this->app_id, "uid:" . $uid, "stime:" . $time, "sign:" . $md5 ]; $ci = curl_init(); curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); curl_setopt($ci, CURLINFO_HEADER_OUT, true); curl_setopt($ci, CURLOPT_HTTPHEADER, $header); // 设置通用传参 curl_setopt($ci, CURLOPT_POST, true); curl_setopt($ci, CURLOPT_POSTFIELDS, $data); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);// 从证书中检查SSL加密算法是否存在 curl_setopt($ci, CURLOPT_URL, $url); $response = curl_exec($ci); $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); curl_close($ci); if (!$http_code || !$response || $http_code !== 200) { throw new Exception('请求失败:' . $http_code); } $result = json_decode($response, true); if ($result['code'] !== 0) { throw new Exception($result['message']); } return $result['data']; } /** * 添加打印设备 */ public function add_printer($sn, $key, $name) { $url = $this->host . '/openapi/addPrinter'; return $this->request($url, [ [ "sn" => $sn, "key" => $key, "name" => $name ] ]); } /** * 修改打印设备 */ public function edit_printer($sn, $name) { $url = $this->host . '/openapi/editPrinter'; return $this->request($url, [ [ "sn" => $sn, "name" => $name ] ]); } /** * 删除打印设备 */ public function delete_printer($sn) { $url = $this->host . '/openapi/delPrinter'; return $this->request($url, [ $sn ]); } /** * 查询打印机状态 */ public function status($sn) { $url = $this->host . '/openapi/getDeviceStatus'; return $this->request($url, [ 'sn' => $sn ]); } /** * 打印小票 */ public function print($sn, $content, $copies, $voice = 4) { $url = $this->host . '/openapi/print'; return $this->request($url, [ "sn" => $sn, "content" => $content, "copies" => $copies, "voice" => $voice, ]); } }