快递100快递查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 查看物流接口
 */
public function wuliu()
{
  $kuaidicom_code = input('kuaidicom_code', ''); // 物流公司code
  $logistics_sn = input('logistics_sn', ''); // 物流编号
  $order_sn = input('order_sn', '');
  $user = request() -> user;
  $uid = $user['uid'];
 
  $order = Order::build() -> where('order_sn', $order_sn)->where('uid', '=', $uid) -> find();
  if (!$order) {
    return json_error('参数无效或订单不存在');
  }
  $state_data = ['在途', '揽收', '疑难', '签收', '派件', '退回', '转单'];
  $kuaidi_res = Kuaidi100::build() -> query($kuaidicom_code, $logistics_sn);
  if ($kuaidi_res['message'] == 'ok') {
    $wuliu = $kuaidi_res;
  }
  $state_text = $state_data[$wuliu['state']] ? $state_data[$wuliu['state']] : '';
  if ($wuliu['state'] === 14) {
    $state_text = '拒签';
  }
  $wuliu['state_text'] = $state_text;
 
  $wuliu['kuaidi_name'] = KuaidiCode::build()->where('code',$wuliu['com'])->value('name');
  return json_success('物流测试', $wuliu);
}

  

 

封装的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
 * @Author: your name
 * @Date: 2022-02-10 11:32:15
 * @LastEditTime: 2022-02-28 10:42:43
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \detection_service\extend\kuaidi100\Kuaidi100.php
 */
namespace kuaidi100;
 
use app\model\system\Config;
class Kuaidi100 {
  //魔术方法
  function __construct() {
    $this -> customer = Config::build() -> getCache('kuaidi100_customer');
    $this -> secret = Config::build() -> getCache('kuaidi100_secret');
    $this -> userid = Config::build() -> getCache('kuaidi100_userid');
    $this -> key = Config::build() -> getCache('kuaidi100_key');
  }
 
  public static function build() {
    return new self();
  }
 
  public function test() {
 
  }
 
  /**
   * 实时查询接口
   */
  public function query($kuaidicom_code, $logistics_sn) {
    //====================================
    // 实时查询示例代码
    // 授权信息可通过链接查看:https://api.kuaidi100.com/manager/page/myinfo/enterprise
    //====================================
 
    //参数设置
    $key = $this -> key;                        //客户授权key
    $customer = $this -> customer;                   //查询公司编号
    $param = array (
      'com' => $kuaidicom_code,             //快递公司编码
      'num' => $logistics_sn,     //快递单号
      'phone' => '',                //手机号
      'from' => '',                 //出发地城市
      'to' => '',                   //目的地城市
      'resultv2' => '1'             //开启行政区域解析
    );
     
    //请求参数
    $post_data = array();
    $post_data["customer"] = $customer;
    $post_data["param"] = json_encode($param);
    $sign = md5($post_data["param"].$key.$post_data["customer"]);
    $post_data["sign"] = strtoupper($sign);
     
    $url = 'http://poll.kuaidi100.com/poll/query.do';    //实时查询请求地址
     
    $params = "";
    foreach ($post_data as $k=>$v) {
        $params .= "$k=".urlencode($v)."&";              //默认UTF-8编码格式
    }
    $post_data = substr($params, 0, -1);
     
    //发送post请求
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    $data = str_replace("\"", '"', $result );
    $data = json_decode($data, true);
 
    return ($data);
  }
}

  

本文作者:潘潘潘的博客

本文链接:https://www.cnblogs.com/seanpan/p/15950116.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   潘潘潘的博客  阅读(147)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.