Kd100.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace extend;
  3. class Kd100
  4. {
  5. private $key; // 授权key
  6. private $customer; // 快递100分配的公司编码
  7. private $http_type;
  8. public function __construct($config){
  9. $this->key = $config["appkey"];
  10. $this->customer = $config["customer"];
  11. $this->http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  12. }
  13. /**
  14. * 查询物流轨迹企业版
  15. * @param unknown $express_no 物流公司编码
  16. * @param unknown $send_no 快递单号
  17. */
  18. public function getExpressTracesEnterpriseEdition($express_no, $send_no){
  19. $data = array();
  20. $data["customer"] = $this->customer;
  21. $data["param"] = '{"com" : "'.$express_no.'", "num" : "'.$send_no.'"}';
  22. $data["sign"] = md5($data["param"].$this->key.$data["customer"]);
  23. $data["sign"] = strtoupper($data["sign"]);
  24. // 测试地址仅可测试100单实际使用时使用“实际地址”
  25. // $url = $this->http_type."poll.kuaidi100.com/test/poll/query.do"; // 测试地址
  26. $url = $this->http_type."poll.kuaidi100.com/poll/query.do"; // 实际地址
  27. $data_url = "";
  28. foreach ($data as $k => $v)
  29. {
  30. $data_url .= "$k=".urlencode($v)."&"; //默认UTF-8编码格式
  31. }
  32. $data = substr($data_url, 0, -1);
  33. //echo "<pre>";print_r($data);exit;
  34. $result = $this->sendRequest($url, 2, $data);
  35. return $result;
  36. }
  37. /**
  38. * 发送请求
  39. * @param unknown $url
  40. * @param unknown $type 1免费版 2企业版
  41. * @param unknown $data
  42. */
  43. public function sendRequest($url, $type, $data = []){
  44. if (function_exists('curl_init') == 1){
  45. $curl = curl_init();
  46. curl_setopt ($curl, CURLOPT_URL, $url);
  47. curl_setopt ($curl, CURLOPT_HEADER,0);
  48. curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
  49. curl_setopt ($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
  50. if(!empty($data)){
  51. curl_setopt($curl, CURLOPT_POST, 1);
  52. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  53. }
  54. curl_setopt ($curl, CURLOPT_TIMEOUT,5);
  55. $result = curl_exec($curl);
  56. $result = json_decode($result, true);
  57. //echo "<pre>";print_r($result);exit;
  58. return $this->handleReturnResult($result);
  59. }else{
  60. return ["success" => false, "reason" => "curl扩展未开启"];
  61. }
  62. }
  63. /**
  64. * 处理返回数据
  65. * @param unknown $data
  66. */
  67. public function handleReturnResult($data){
  68. $result = array();
  69. // 处理100企业版返回数据
  70. if(isset($data["result"])){
  71. $result["success"] = false;
  72. $result["reason"] = $data["message"];
  73. }else{
  74. $result["success"] = true;
  75. $result["reason"] = $data["message"];
  76. $result["status"] = $data["state"];
  77. $result["status_name"] = $this->getStatusName($data["state"]);
  78. $result["shipper_code"] = $data["com"];
  79. $result["logistic_code"] = $data["nu"];
  80. $list = [];
  81. foreach($data["data"] as $k => $v){
  82. $list[] = array(
  83. "datetime" => $v["ftime"],
  84. "remark" => $v["context"],
  85. );
  86. }
  87. //echo "<pre>";print_r($list);exit;
  88. //$result["$list"] = $list;
  89. $result["list"] = $list;
  90. }
  91. return $result;
  92. }
  93. /**
  94. * 物流跟踪状态
  95. * @param $state
  96. */
  97. public function getStatusName($status){
  98. $data = array(
  99. 0 => "在途",
  100. 1 => "揽收",
  101. 2 => "疑难",
  102. 3 => "签收",
  103. 4 => "退签",
  104. 5 => "派件",
  105. 6 => "退回",
  106. );
  107. $status_name = isset($data[$status]) ? $data[$status] : '';
  108. return $status_name;
  109. }
  110. }