Kdniaoapi.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\model;
  3. class Kdniaoapi {
  4. //调用查询物流轨迹
  5. //---------------------------------------------
  6. //$logisticResult =getOrderTracesByJson();
  7. //echo logisticResult;
  8. //---------------------------------------------
  9. /**
  10. * 查询订单物流轨迹 返回数组
  11. */
  12. function getOrderTracesByArray($data) {
  13. $arraydata = json_decode($this->getOrderTracesByJson($data), true);
  14. //var_dump($arraydata);
  15. if (!empty($arraydata['Traces'])) {
  16. $arraydata['Traces'] = array_reverse($arraydata['Traces']);
  17. }
  18. return $arraydata;
  19. }
  20. /**
  21. * Json方式 查询订单物流轨迹
  22. */
  23. function getOrderTracesByJson($data) {
  24. $ReqURL = 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
  25. $requestData = "{'OrderCode':'" . $data['OrderCode'] . "','ShipperCode':'" . $data['ShipperCode'] . "','LogisticCode':'" . $data['LogisticCode'] . "'}";
  26. $datas = array(
  27. 'EBusinessID' => trim(Config::getconfig('transport')['kdniao_id']),
  28. 'RequestType' => '1002',
  29. 'RequestData' => urlencode($requestData),
  30. 'DataType' => '2',
  31. );
  32. $datas['DataSign'] = $this->encrypt($requestData, trim(Config::getconfig('transport')['kdniao_apikey']));
  33. $result = $this->sendPost($ReqURL, $datas);
  34. //根据公司业务处理返回的信息......
  35. return $result;
  36. }
  37. /**
  38. * post提交数据
  39. * @param string $url 请求Url
  40. * @param array $datas 提交的数据
  41. * @return url响应返回的html
  42. */
  43. function sendPost($url, $datas) {
  44. $temps = array();
  45. foreach ($datas as $key => $value) {
  46. $temps[] = sprintf('%s=%s', $key, $value);
  47. }
  48. if(!empty($temps)){
  49. $post_data = implode('&', $temps);
  50. }
  51. $url_info = parse_url($url);
  52. if (empty($url_info['port'])) {
  53. $url_info['port'] = 80;
  54. }
  55. $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  56. $httpheader .= "Host:" . $url_info['host'] . "\r\n";
  57. $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
  58. $httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
  59. $httpheader .= "Connection:close\r\n\r\n";
  60. $httpheader .= $post_data;
  61. $fd = fsockopen($url_info['host'], $url_info['port']);
  62. fwrite($fd, $httpheader);
  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. function encrypt($data, $appkey) {
  83. return urlencode(base64_encode(md5($data . $appkey)));
  84. }
  85. }