快遞公司秘车,只要直接輸入快遞單號就可以自動識別快遞單號所在快遞公司和物流信息,還是非常方便的,只要幾行代碼就可以完美的集成到你系統(tǒng)的功能中了卵惦!
github: https://github.com/whm19940308/Express
個人博客:https://www.whmblog.cn/
使用示例: 使用如下记靡,只需要調(diào)用類中的getLogisticsInfo()方法谈竿,參數(shù)傳入訂單號即可 $e = new Express();
$data = $e->getLogisticsInfo("453371918456");
echo '<pre>';
var_dump($data);
<?php
/**
* Express.class.php 快遞查詢類
*
* @author 王浩銘
* @date 2017/09/27
*/
class Express {
/**
* @desc 采集網(wǎng)頁內(nèi)容的方法,建議使用curl摸吠,效率更高
* @param $url
* @return mixed|string
*/
private function getContent($url){
if(function_exists("file_get_contents")){
$file_contents = file_get_contents($url);
}else{
$ch = curl_init();
$timeout = 5; // 設(shè)置5秒超時
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
/**
* @desc 得到目前物流單號可能存在的快遞公司
* @param string $order_no
* @return mixed
*/
public function getOrder($order_no=''){
$result = $this->getContent("http://www.kuaidi100.com/autonumber/autoComNum?text=".$order_no);
$data = json_decode($result,true);
return $data;
}
/**
* @desc http://www.kuaidi100.com/query?type=zhongtong&postid=453371918456&id=1&valicode=&temp=0.40349807080624434
* @desc 返回的數(shù)據(jù)結(jié)果參考官方文檔:https://www.kuaidi100.com/openapi/api_post.shtml
* @desc 直接調(diào)用該方法,傳入物流單號即可查詢物流信息
* @param string $order_no
* @return bool|mixed
*/
public function getLogisticsInfo($order_no=''){
$result = $this->getOrder($order_no);
$auto_arr = $result['auto'];
if(count($auto_arr)>0){
foreach ($auto_arr as $key => $value){
$temp = $this->randFloat();
$comCode = $value['comCode'];
$url = "http://www.kuaidi100.com/query?type=$comCode&postid=$order_no&id=1&valicode=&temp=$temp";// $temp 隨機(jī)數(shù),防止緩存
$json = $this->getContent($url);
$data = json_decode($json,true);
if($data['message']=='ok'){
return $data;
}
}
}
return false;
}
/**
* 生成0~1隨機(jī)小數(shù)
* @param Int $min
* @param Int $max
* @return Float
*/
function randFloat($min=0, $max=1){
return $min + mt_rand()/mt_getrandmax() * ($max-$min);
}
}
github: https://github.com/whm19940308/Express
更多精彩請訪問個人博客:https://www.whmblog.cn/
轉(zhuǎn)載請標(biāo)明原文鏈接:http://www.reibang.com/p/62fe9a9a9248