扩展接口 - kuaidi100查询接口对接
in PHP with 0 comment

扩展接口 - kuaidi100查询接口对接

in PHP with 0 comment

扩展接口 - kuaidi100对接

说明

商城项目中用到了快递查询服务,记录一下快递100查询接口的使用


介绍

官网地址:https://www.kuaidi100.com/
接口文档:https://poll.kuaidi100.com/manager/page/document/synquery

官网

查询接口

1.接口地址:https://poll.kuaidi100.com/poll/query.do
2.请求类型:POST
3.请求参数:

快递接口参数

4.请求示例:

customer=89dfcabd892kdjmkg43opui
sign=FOI09kDFNBTRGKD0PUYDS
param={
    "com":"ems",
    "num":"em263999513jp",
    "phone":"13868688888",
    "from":"广东省深圳市南山区",
    "to":"北京市朝阳区",
    "key":"XXX ",
    "resultv2":"1",
    "show":"0",
    "order":"desc"
}

5.返回结果:

请输入图片描述

6.返回数据示例

{
    "message":"ok",
    "state":"0",
    "status":"200",
    "condition":"F00",
    "ischeck":"0",
    "com":"yuantong",
    "nu":"V030344422",
    "data":[
        {
            "context":"上海分拨中心/装件入车扫描 ",
            "time":"2012-08-28 16:33:19",
            "ftime":"2012-08-28 16:33:19",
        },
        {
            "context":"上海分拨中心/下车扫描 ",
            "time":"2012-08-27 23:22:42",
            "ftime":"2012-08-27 23:22:42",
        }]
}

代码示例

控制层
   public function actionKuaidi100(){
      $order_number  = htmlspecialchars(\Yii::$app->request->post('code'));
      $data = OrdersDispatch::find()->where(['order_number' => $order_number])->one();
      if(empty($data['dispatch_code']) || empty($data['dispatch_number'])){
        return $this->returnData(0, '操作失败', '快递公司和快递单号不能为空');
      }
      //30分钟内不需要重新请求
      $time = time();
      $difftime = $data['dispatch_updated']+30*60;
      $statusSkip = array(3,4,2);

  if($difftime > $time || in_array($data['dispatch_status'],$statusSkip)){ //不需要重新调用接口 已经结束的不需要重新调用接口
    $dispatchData = empty($data['dispatch_data'])?[]:json_decode($data['dispatch_data'],true);
    return $this->returnData(1, '1', $dispatchData['data']);
  }else{
    $KDdatas = $this->kuaidi100($data['dispatch_code'],$data['dispatch_number']);
    //更新快递状态
    if(!$KDdatas){
      return $this->returnData(0, '数据不存在', '没有检索到相关数据');
    }
    $data->dispatch_status = $KDdatas['state'];
    $data->dispatch_updated = $time;
    $data->dispatch_data = json_encode($KDdatas);
    if(!$data->save()){
      return $this->returnData(0, '异常错误', $KDdatas['state']);
    }

    return $this->returnData(1, '', $KDdatas['data']);
  }
} 
发送接口
  private function kuaidi100($com,$num){
    //参数设置
    $key = 'OIgW******';                        //客户授权key
    $customer = 'D8F55960CD***************';                    //查询公司编号
    $param = array (
      'com' => $com,            //快递公司编码
      'num' => $num,    //快递单号
      '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);
    // echo '请求参数<br/>'.$post_data;

    //发送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;
  }

结尾

没有人可以回到过去重新开始,但谁都可以从现在开始,书写一个全然不同的结局。

Responses