首先你要去申请一个快递鸟的账号:http://www.kdniao.com/reg,申请账号没有什么技术含量,就不说了 登陆后,打开你的个人中心,用户id就是程序中要用到的商户id,然后apikey就是程序中要用到的appkey 常用的快递公司编码如下图,更多的自己去快递鸟查看
下面是控制器 <?php namespace xcx\modules\v1\controllers; use yii\rest\Controller; use common\models\PurchaseOrder; class LogisticsController extends Controller { private $EBusinessID='这里换成你自己的电商id';电商ID private $AppKey='这里换成你自己的appkey';//电商加密私钥,快递鸟提供,注意保管,不要泄漏 private $ReqURL='http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';//请求url //ShipperCode,快递公司的编码,//LogisticCode,快递单号 public function actionLogistics($ShipperCode='SF',$LogisticCode='146364546565') { $logisticResult=$this->getOrderTracesByJson($ShipperCode,$LogisticCode); $result=json_decode($logisticResult); return $result; } /** * Json方式 查询订单物流轨迹 */ function getOrderTracesByJson($ShipperCode,$LogisticCode){ $requestData= '{"OrderCode":"","ShipperCode":"'.$ShipperCode.'","LogisticCode":"'.$LogisticCode.'"}'; $datas = array( 'EBusinessID' => $this->EBusinessID, 'RequestType' => '1002', 'RequestData' => urlencode($requestData) , 'DataType' => '2', ); $datas['DataSign'] = $this->encrypt($requestData, $this->AppKey); $result=$this->sendPost($this->ReqURL, $datas); return $result; } /** * post提交数据 * @param string $url 请求Url * @param array $datas 提交的数据 * @return url响应返回的html */ function sendPost($url, $datas) { $temps = array(); foreach ($datas as $key => $value) { $temps[] = sprintf('%s=%s', $key, $value); } $post_data = implode('&', $temps); $url_info = parse_url($url); if(empty($url_info['port'])) { $url_info['port']=80; } $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n"; $httpheader.= "Host:" . $url_info['host'] . "\r\n"; $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n"; $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n"; $httpheader.= "Connection:close\r\n\r\n"; $httpheader.= $post_data; $fd = fsockopen($url_info['host'], $url_info['port']); fwrite($fd, $httpheader); $gets = ""; $headerFlag = true; while (!feof($fd)) { if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) { break; } } while (!feof($fd)) { $gets.= fread($fd, 128); } fclose($fd); return $gets; } /** * 电商Sign签名生成 * @param data 内容 * @param appkey Appkey * @return DataSign签名 */ function encrypt($data, $appkey) { return urlencode(base64_encode(md5($data.$appkey))); } }