PHP后台 與前端 對接商品物流信息的接口
運用的 快遞100的API接口
public function getExpress() { $user_id = input('post.user_id'); if(null===$user_id){ $this->json_error('請傳過來用戶編號'); } $com = input('post.expresscom'); $num = input('post.expresssn'); // $com = 'zhongtong'; // $num = '73124309026125'; $key = Config::get('kuaidi')['key']; //客戶授權key $customer = Config::get('kuaidi')['cus']; //查詢公司編號 $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); $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['name'] = config('system.express_company')[$com]['statusname']; $data = json_decode($data,true); $data['name'] = config('system.express_company')[$com]['statusname']; $data = json_encode($data); $data = json_decode($data); $this->json_success($data); }
如下圖所示