1. 程式人生 > 其它 >快遞100快遞查詢

快遞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);
  }
}