KdOrder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use app\models\Option;
  9. class KdOrder
  10. {
  11. /**
  12. * Json方式 调用电子面单接口
  13. *
  14. */
  15. public static function submitEOrder($requestData, $store_id, $supplier_id = 0)
  16. {
  17. $EBusinessID = Option::get('kdniao_mch_id', $store_id, 'store')['value'];
  18. $AppKey = Option::get('kdniao_api_key', $store_id, 'store')['value'];
  19. if ($supplier_id) {
  20. $EBusinessID = Option::get('kdniao_mch_id', $supplier_id, 'supplier')['value'];
  21. $AppKey = Option::get('kdniao_api_key', $supplier_id, 'supplier')['value'];
  22. }
  23. // 测试接口
  24. $ReqUrl = 'http://testapi.kdniao.com:8081/api/EOrderService ';
  25. // 正式接口
  26. $ReqUrl = 'http://api.kdniao.com/api/EOrderService';
  27. $data = array(
  28. 'EBusinessID' => $EBusinessID,
  29. 'RequestType' => '1007',
  30. 'RequestData' => urlencode($requestData) ,
  31. 'DataType' => '2',
  32. );
  33. $data['DataSign'] = self::encrypt($requestData, $AppKey);
  34. $result=self::sendPost($ReqUrl, $data);
  35. //根据公司业务处理返回的信息......
  36. return $result;
  37. }
  38. /**
  39. * post提交数据
  40. * @param string $url 请求Url
  41. * @param array $datas 提交的数据
  42. * @return url响应返回的html
  43. */
  44. public static function sendPost($url, $datas)
  45. {
  46. $temps = array();
  47. foreach ($datas as $key => $value) {
  48. $temps[] = sprintf('%s=%s', $key, $value);
  49. }
  50. $post_data = implode('&', $temps);
  51. $url_info = parse_url($url);
  52. if (empty($url_info['port'])) {
  53. $url_info['port']=80;
  54. }
  55. $http_header = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  56. $http_header.= "Host:" . $url_info['host'] . "\r\n";
  57. $http_header.= "Content-Type:application/x-www-form-urlencoded\r\n";
  58. $http_header.= "Content-Length:" . strlen($post_data) . "\r\n";
  59. $http_header.= "Connection:close\r\n\r\n";
  60. $http_header.= $post_data;
  61. $fd = fsockopen($url_info['host'], $url_info['port']);
  62. fwrite($fd, $http_header);
  63. $gets = "";
  64. $headerFlag = true;
  65. while (!feof($fd)) {
  66. if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  67. break;
  68. }
  69. }
  70. while (!feof($fd)) {
  71. $gets.= fread($fd, 128);
  72. }
  73. fclose($fd);
  74. return $gets;
  75. }
  76. /**
  77. * 电商Sign签名生成
  78. * @param data 内容
  79. * @param appkey Appkey
  80. * @return DataSign签名
  81. */
  82. public static function encrypt($data, $appkey)
  83. {
  84. return urlencode(base64_encode(md5($data.$appkey)));
  85. }
  86. /**************************************************************
  87. *
  88. * 使用特定function对数组中所有元素做处理
  89. * @param string &$array 要处理的字符串
  90. * @param string $function 要执行的函数
  91. * @return boolean $apply_to_keys_also 是否也应用到key上
  92. * @access public
  93. *
  94. *************************************************************/
  95. function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
  96. {
  97. static $recursive_counter = 0;
  98. if (++$recursive_counter > 1000) {
  99. die('possible deep recursion attack');
  100. }
  101. foreach ($array as $key => $value) {
  102. if (is_array($value)) {
  103. self::arrayRecursive($array[$key], $function, $apply_to_keys_also);
  104. } else {
  105. $array[$key] = $function($value);
  106. }
  107. if ($apply_to_keys_also && is_string($key)) {
  108. $new_key = $function($key);
  109. if ($new_key != $key) {
  110. $array[$new_key] = $array[$key];
  111. unset($array[$key]);
  112. }
  113. }
  114. }
  115. $recursive_counter--;
  116. }
  117. /**************************************************************
  118. *
  119. * 将数组转换为JSON字符串(兼容中文)
  120. * @param array $array 要转换的数组
  121. * @return string 转换得到的json字符串
  122. * @access public
  123. *
  124. *************************************************************/
  125. function JSON($array)
  126. {
  127. self::arrayRecursive($array, 'urlencode', true);
  128. $json = json_encode($array);
  129. return urldecode($json);
  130. }
  131. }