[PHP] EMS代收货款接口对接demo →→→→→进入此内容的聊天室

来自 , 2021-01-08, 写在 PHP, 查看 205 次.
URL http://www.code666.cn/view/8643c8e2
  1. <?php
  2. /**
  3.  *
  4.  * 快递鸟电子面单接口
  5.  *
  6.  * @技术QQ: 4009633321
  7.  * @技术QQ群: 200121393
  8.  * @see: http://www.kdniao.com/MiandanAPI.aspx
  9.  * @copyright: 深圳市快金数据技术服务有限公司
  10.  *
  11.  * ID和Key请到官网申请:http://www.kdniao.com/ServiceApply.aspx
  12.  */
  13.  
  14. //电商ID
  15. defined('EBusinessID') or define('EBusinessID', '请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx');
  16. //电商加密私钥,快递鸟提供,注意保管,不要泄漏
  17. defined('AppKey') or define('AppKey', '请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx');
  18. //请求url,接口正式地址:http://api.kdniao.cc/api/Eorderservice
  19. defined('ReqURL') or define('ReqURL', 'http://testapi.kdniao.cc:8081/api/Eorderservice');
  20.  
  21.  
  22. //调用获取物流轨迹
  23. //-------------------------------------------------------------
  24.  
  25. //构造电子面单提交信息
  26. $eorder = [];
  27. $eorder["ShipperCode"] = "SF";
  28. $eorder["OrderCode"] = "PM201604062341";
  29. $eorder["PayType"] = 1;
  30. $eorder["ExpType"] = 1;
  31.  
  32. $sender = [];
  33. $sender["Name"] = "李先生";
  34. $sender["Mobile"] = "18888888888";
  35. $sender["ProvinceName"] = "李先生";
  36. $sender["CityName"] = "深圳市";
  37. $sender["ExpAreaName"] = "福田区";
  38. $sender["Address"] = "赛格广场5401AB";
  39.  
  40. $receiver = [];
  41. $receiver["Name"] = "李先生";
  42. $receiver["Mobile"] = "18888888888";
  43. $receiver["ProvinceName"] = "李先生";
  44. $receiver["CityName"] = "深圳市";
  45. $receiver["ExpAreaName"] = "福田区";
  46. $receiver["Address"] = "赛格广场5401AB";
  47.  
  48. $commodityOne = [];
  49. $commodityOne["GoodsName"] = "其他";
  50. $commodity = [];
  51. $commodity[] = $commodityOne;
  52.  
  53. $eorder["Sender"] = $sender;
  54. $eorder["Receiver"] = $receiver;
  55. $eorder["Commodity"] = $commodity;
  56.  
  57.  
  58. //调用电子面单
  59. $jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
  60.  
  61. //$jsonParam = JSON($eorder);//兼容php5.2(含)以下
  62.  
  63. echo "电子面单接口提交内容:<br/>".$jsonParam;
  64. $jsonResult = submitEOrder($jsonParam);
  65. echo "<br/><br/>电子面单提交结果:<br/>".$jsonResult;
  66.  
  67. //解析电子面单返回结果
  68. $result = json_decode($jsonResult, true);
  69. echo "<br/><br/>返回码:".$result["ResultCode"];
  70. if($result["ResultCode"] == "100") {
  71.         echo "<br/>是否成功:".$result["Success"];
  72. }
  73. else {
  74.         echo "<br/>电子面单下单失败";
  75. }
  76. //-------------------------------------------------------------
  77.  
  78.  
  79. /**
  80.  * Json方式 查询订单物流轨迹
  81.  */
  82. function submitEOrder($requestData){
  83.         $datas = array(
  84.         'EBusinessID' => EBusinessID,
  85.         'RequestType' => '1007',
  86.         'RequestData' => urlencode($requestData) ,
  87.         'DataType' => '2',
  88.     );
  89.     $datas['DataSign'] = encrypt($requestData, AppKey);
  90.         $result=sendPost(ReqURL, $datas);      
  91.        
  92.         //根据公司业务处理返回的信息......
  93.        
  94.         return $result;
  95. }
  96.  
  97.  
  98. /**
  99.  *  post提交数据
  100.  * @param  string $url 请求Url
  101.  * @param  array $datas 提交的数据
  102.  * @return url响应返回的html
  103.  */
  104. function sendPost($url, $datas) {
  105.     $temps = array();  
  106.     foreach ($datas as $key => $value) {
  107.         $temps[] = sprintf('%s=%s', $key, $value);             
  108.     }  
  109.     $post_data = implode('&', $temps);
  110.     $url_info = parse_url($url);
  111.         if($url_info['port']=='')
  112.         {
  113.                 $url_info['port']=80;  
  114.         }
  115.         echo $url_info['port'];
  116.     $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  117.     $httpheader.= "Host:" . $url_info['host'] . "\r\n";
  118.     $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
  119.     $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
  120.     $httpheader.= "Connection:close\r\n\r\n";
  121.     $httpheader.= $post_data;
  122.     $fd = fsockopen($url_info['host'], $url_info['port']);
  123.     fwrite($fd, $httpheader);
  124.     $gets = "";
  125.         $headerFlag = true;
  126.         while (!feof($fd)) {
  127.                 if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  128.                         break;
  129.                 }
  130.         }
  131.     while (!feof($fd)) {
  132.                 $gets.= fread($fd, 128);
  133.     }
  134.     fclose($fd);  
  135.    
  136.     return $gets;
  137. }
  138.  
  139. /**
  140.  * 电商Sign签名生成
  141.  * @param data 内容  
  142.  * @param appkey Appkey
  143.  * @return DataSign签名
  144.  */
  145. function encrypt($data, $appkey) {
  146.     return urlencode(base64_encode(md5($data.$appkey)));
  147. }
  148. /**************************************************************
  149.  *
  150.  *  使用特定function对数组中所有元素做处理
  151.  *  @param  string  &$array     要处理的字符串
  152.  *  @param  string  $function   要执行的函数
  153.  *  @return boolean $apply_to_keys_also     是否也应用到key上
  154.  *  @access public
  155.  *
  156.  *************************************************************/  
  157. function arrayRecursive(&$array, $function, $apply_to_keys_also = false)  
  158. {  
  159.     static $recursive_counter = 0;  
  160.     if (++$recursive_counter > 1000) {  
  161.         die('possible deep recursion attack');  
  162.     }  
  163.     foreach ($array as $key => $value) {  
  164.         if (is_array($value)) {  
  165.             arrayRecursive($array[$key], $function, $apply_to_keys_also);  
  166.         } else {  
  167.             $array[$key] = $function($value);  
  168.         }  
  169.    
  170.         if ($apply_to_keys_also && is_string($key)) {  
  171.             $new_key = $function($key);  
  172.             if ($new_key != $key) {  
  173.                 $array[$new_key] = $array[$key];  
  174.                 unset($array[$key]);  
  175.             }  
  176.         }  
  177.     }  
  178.     $recursive_counter--;  
  179. }  
  180.  
  181.  
  182. /**************************************************************
  183.  *
  184.  *  将数组转换为JSON字符串(兼容中文)
  185.  *  @param  array   $array      要转换的数组
  186.  *  @return string      转换得到的json字符串
  187.  *  @access public
  188.  *
  189.  *************************************************************/  
  190. function JSON($array) {  
  191.     arrayRecursive($array, 'urlencode', true);  
  192.     $json = json_encode($array);  
  193.     return urldecode($json);  
  194. }  
  195. ?>

回复 "EMS代收货款接口对接demo"

这儿你可以回复上面这条便签

captcha