快递100快递查询

  /**
   * 查看物流接口
   */
  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);
  }

  

 

封装的方法

<?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);
  }
}

  

posted @ 2022-03-01 14:44  潘潘潘的博客  阅读(140)  评论(0编辑  收藏  举报