我们还需要把$requestData进行加密处理,也就是encrypt方法。
/*
* 进行加密
*/
function encrypt($data, $appkey) {
return urlencode(base64_encode(md5($data.$appkey)));
}
加密过后就直接通过ReqURL进行访问,返回的数据就是物流信息。

源代码
<?php
/**
* 使用快递鸟api进行查询
* User: Administrator
* Date: 2017/4/22 0022
* Time: 09:09
*/
class KuaidiController{
const EBusinessID = 1285564;
const AppKey = '264ff9e0-2f4c-48d5-877f-1e0670400d18';
const ReqURL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
/**
* @param $ShipperCode 快递公司编号
* @param $order_sn 运单号
*/
public function getMessage($ShipperCode,$order_sn){
$requestData= "{'OrderCode':'','ShipperCode':'".$ShipperCode."','LogisticCode':'".$order_sn."'}";
$datas = array(
'EBusinessID' => self::EBusinessID,
'RequestType' => '1002',//接口指令1002,固定
'RequestData' => urlencode($requestData) ,
'DataType' => '2', //数据返回格式 2 json
);
//把$requestData进行加密处理
$datas['DataSign'] = $this -> encrypt($requestData, self::AppKey);
$result = $this -> sendPost( self::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.0rn";
$httpheader.= "Host:" . $url_info['host'] . "rn";
$httpheader.= "Content-Type:application/x-www-form-urlencodedrn";
$httpheader.= "Content-Length:" . strlen($post_data) . "rn";
$httpheader.= "Connection:closernrn";
$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 == "rn" || $header == "n")) {
break;
}
}
while (!feof($fd)) {
$gets.= fread($fd, 128);
}
fclose($fd);
return $gets;
}
/*
* 进行加密
*/
function encrypt($data, $appkey) {
return urlencode(base64_encode(md5($data.$appkey)));
}
}
$model = new KuaidiController();
$res = $model -> getMessage('ZTO','12345678');
echo "<pre>";
var_dump($res);
以上所述是小编给大家介绍的PHP使用第三方即时获取物流动态,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易采站长站网站的支持!







