[PHP] 中通电子面单申请及对接demo →→→→→进入此内容的聊天室

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

回复 "中通电子面单申请及对接demo"

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

captcha