Kdniao.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace extend;
  3. class Kdniao
  4. {
  5. private $EBusinessID; // 授权key
  6. private $AppKey; // 快递100分配的公司编码
  7. private $order_url = 'https://api.kdniao.com/api/Eorderservice'; //电子面单url
  8. private $print_url = 'https://www.kdniao.com/External/PrintOrder.aspx'; //电子面单批量打印url
  9. public function __construct($config){
  10. $this->EBusinessID = $config["EBusinessID"];
  11. $this->AppKey = $config["AppKey"];
  12. }
  13. /**
  14. * Json方式 调用电子面单接口
  15. */
  16. public function submitEOrder($requestData)
  17. {
  18. $jsonParam = json_encode($requestData, JSON_UNESCAPED_UNICODE);
  19. $datas = array(
  20. 'EBusinessID' => $this->EBusinessID,
  21. 'RequestType' => '1007',
  22. 'RequestData' => urlencode($jsonParam),
  23. 'DataType' => '2',
  24. );
  25. $datas['DataSign'] = $this->encrypt($jsonParam, $this->AppKey);
  26. $result = $this->sendPost($this->order_url, $datas);
  27. return $result;
  28. }
  29. /**
  30. * 批量打印电子面单
  31. * @param $data
  32. */
  33. public function build_form($data)
  34. {
  35. // OrderCode:需要打印的订单号,和调用快递鸟电子面单的订单号一致,PortName:本地打印机名称,请参考使用手册设置打印机名称。支持多打印机同时打印。
  36. // $request_data = '[{"OrderCode":"2020051919550001","PortName":"HP LaserJet Pro MFP M125-M126 PCLmS"},{"OrderCode":"2020052009440001","PortName":"HP LaserJet Pro MFP M125-M126 PCLmS"}]';
  37. $request_data_encode = urlencode($data);
  38. // 如果报数据验证失败 请检查此处get_ip()返回的IP 是否与调用打印的【客户机】IP一致 可以写死IP尝试
  39. $data_sign = $this->encrypt($this->get_ip() . $data, APIKey);
  40. $is_priview = '0'; //是否预览,0-不预览 1-预览
  41. // 组装表单
  42. $form = '<form id="form1" method="POST" action="' . $this->print_url . '"><input type="text" name="RequestData" value="' . $request_data_encode . '"/><input type="text" name="EBusinessID" value="' . $this->EBusinessID . '"/><input type="text" name="DataSign" value="' . $data_sign . '"/><input type="text" name="IsPriview" value="' . $is_priview . '"/></form><script>form1.submit();</script>';
  43. print_r($form);
  44. }
  45. /**
  46. * post提交数据
  47. * @param string $url 请求Url
  48. * @param array $datas 提交的数据
  49. * @return url响应返回的html
  50. */
  51. public function sendPost($url, $datas)
  52. {
  53. $temps = array();
  54. foreach ($datas as $key => $value) {
  55. $temps[] = sprintf('%s=%s', $key, $value);
  56. }
  57. $post_data = implode('&', $temps);
  58. $url_info = parse_url($url);
  59. if (empty($url_info['port'])) {
  60. $url_info['port'] = 80;
  61. }
  62. $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  63. $httpheader .= "Host:" . $url_info['host'] . "\r\n";
  64. $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
  65. $httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
  66. $httpheader .= "Connection:close\r\n\r\n";
  67. $httpheader .= $post_data;
  68. $fd = fsockopen($url_info['host'], $url_info['port']);
  69. fwrite($fd, $httpheader);
  70. $gets = "";
  71. $headerFlag = true;
  72. while (!feof($fd)) {
  73. if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  74. break;
  75. }
  76. }
  77. while (!feof($fd)) {
  78. $gets .= fread($fd, 128);
  79. }
  80. fclose($fd);
  81. return $gets;
  82. }
  83. /**
  84. * 电商Sign签名生成
  85. * @param data 内容
  86. * @param appkey Appkey
  87. * @return DataSign签名
  88. */
  89. public function encrypt($data, $appkey)
  90. {
  91. return urlencode(base64_encode(md5($data.$appkey)));
  92. }
  93. /**************************************************************
  94. *
  95. * 使用特定function对数组中所有元素做处理
  96. * @param string &$array 要处理的字符串
  97. * @param string $function 要执行的函数
  98. * @return boolean $apply_to_keys_also 是否也应用到key上
  99. * @access public
  100. *
  101. *************************************************************/
  102. public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
  103. {
  104. static $recursive_counter = 0;
  105. if (++$recursive_counter > 1000) {
  106. die('possible deep recursion attack');
  107. }
  108. foreach ($array as $key => $value) {
  109. if (is_array($value)) {
  110. $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
  111. } else {
  112. $array[$key] = $function($value);
  113. }
  114. if ($apply_to_keys_also && is_string($key)) {
  115. $new_key = $function($key);
  116. if ($new_key != $key) {
  117. $array[$new_key] = $array[$key];
  118. unset($array[$key]);
  119. }
  120. }
  121. }
  122. $recursive_counter--;
  123. }
  124. /**************************************************************
  125. *
  126. * 将数组转换为JSON字符串(兼容中文)
  127. * @param array $array 要转换的数组
  128. * @return string 转换得到的json字符串
  129. * @access public
  130. *
  131. *************************************************************/
  132. public function JSON($array)
  133. {
  134. $this->arrayRecursive($array, 'urlencode', true);
  135. $json = json_encode($array);
  136. return urldecode($json);
  137. }
  138. /**
  139. * 判断是否为内网IP
  140. * @param string IP
  141. * @return boolean 是否内网IP
  142. */
  143. public function is_private_ip($ip)
  144. {
  145. return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
  146. }
  147. /**
  148. * 获取客户端IP(非用户服务器IP)
  149. * @return string 客户端IP
  150. */
  151. public function get_ip()
  152. {
  153. $ip = null;
  154. // 获取客户端IP
  155. if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  156. $ip = getenv('HTTP_CLIENT_IP');
  157. } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  158. $ip = getenv('HTTP_X_FORWARDED_FOR');
  159. } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  160. $ip = getenv('REMOTE_ADDR');
  161. } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  162. $ip = $_SERVER['REMOTE_ADDR'];
  163. }
  164. // 判断是否获取到 如果获取到本地IP地址 需要从外网获取本机地址
  165. if ($ip == '' || $this->is_private_ip($ip) == false) {
  166. die('无法正确获取当前机器IP');
  167. } else {
  168. return trim(strip_tags($ip));
  169. }
  170. }
  171. }