| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils;
- use app\models\Option;
- class KdOrder
- {
- /**
- * Json方式 调用电子面单接口
- *
- */
- public static function submitEOrder($requestData, $store_id, $supplier_id = 0)
- {
- $EBusinessID = Option::get('kdniao_mch_id', $store_id, 'store')['value'];
- $AppKey = Option::get('kdniao_api_key', $store_id, 'store')['value'];
- if ($supplier_id) {
- $EBusinessID = Option::get('kdniao_mch_id', $supplier_id, 'supplier')['value'];
- $AppKey = Option::get('kdniao_api_key', $supplier_id, 'supplier')['value'];
- }
- // 测试接口
- $ReqUrl = 'http://testapi.kdniao.com:8081/api/EOrderService ';
- // 正式接口
- $ReqUrl = 'http://api.kdniao.com/api/EOrderService';
- $data = array(
- 'EBusinessID' => $EBusinessID,
- 'RequestType' => '1007',
- 'RequestData' => urlencode($requestData) ,
- 'DataType' => '2',
- );
- $data['DataSign'] = self::encrypt($requestData, $AppKey);
- $result=self::sendPost($ReqUrl, $data);
- //根据公司业务处理返回的信息......
- return $result;
- }
- /**
- * post提交数据
- * @param string $url 请求Url
- * @param array $datas 提交的数据
- * @return url响应返回的html
- */
- public static function sendPost($url, $datas)
- {
- $temps = array();
- foreach ($datas as $key => $value) {
- $temps[] = sprintf('%s=%s', $key, $value);
- }
- $post_data = implode('&', $temps);
- $url_info = parse_url($url);
- if (empty($url_info['port'])) {
- $url_info['port']=80;
- }
- $http_header = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
- $http_header.= "Host:" . $url_info['host'] . "\r\n";
- $http_header.= "Content-Type:application/x-www-form-urlencoded\r\n";
- $http_header.= "Content-Length:" . strlen($post_data) . "\r\n";
- $http_header.= "Connection:close\r\n\r\n";
- $http_header.= $post_data;
- $fd = fsockopen($url_info['host'], $url_info['port']);
- fwrite($fd, $http_header);
- $gets = "";
- $headerFlag = true;
- while (!feof($fd)) {
- if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
- break;
- }
- }
- while (!feof($fd)) {
- $gets.= fread($fd, 128);
- }
- fclose($fd);
- return $gets;
- }
- /**
- * 电商Sign签名生成
- * @param data 内容
- * @param appkey Appkey
- * @return DataSign签名
- */
- public static function encrypt($data, $appkey)
- {
- return urlencode(base64_encode(md5($data.$appkey)));
- }
- /**************************************************************
- *
- * 使用特定function对数组中所有元素做处理
- * @param string &$array 要处理的字符串
- * @param string $function 要执行的函数
- * @return boolean $apply_to_keys_also 是否也应用到key上
- * @access public
- *
- *************************************************************/
- function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
- {
- static $recursive_counter = 0;
- if (++$recursive_counter > 1000) {
- die('possible deep recursion attack');
- }
- foreach ($array as $key => $value) {
- if (is_array($value)) {
- self::arrayRecursive($array[$key], $function, $apply_to_keys_also);
- } else {
- $array[$key] = $function($value);
- }
- if ($apply_to_keys_also && is_string($key)) {
- $new_key = $function($key);
- if ($new_key != $key) {
- $array[$new_key] = $array[$key];
- unset($array[$key]);
- }
- }
- }
- $recursive_counter--;
- }
- /**************************************************************
- *
- * 将数组转换为JSON字符串(兼容中文)
- * @param array $array 要转换的数组
- * @return string 转换得到的json字符串
- * @access public
- *
- *************************************************************/
- function JSON($array)
- {
- self::arrayRecursive($array, 'urlencode', true);
- $json = json_encode($array);
- return urldecode($json);
- }
- }
|