首頁  >  文章  >  後端開發  >  關於PHP快遞查詢類

關於PHP快遞查詢類

jacklove
jacklove原創
2018-05-22 09:30:172344瀏覽

本篇將會介紹PHP快遞查詢類別的相關方法。

快遞公司,只要直接輸入快遞單號就可以自動識別快遞單號所在快遞公司和物流信息,還是非常方便的,只要幾行代碼就可以完美的集成到你係統的功能中了!

使用範例: 使用如下,只需呼叫類別中的getLogisticsInfo()方法,參數傳入訂單號碼即可$e = new Express();

$data = $e->getLogisticsInfo("453371918456");
echo &#39;<pre class="brush:php;toolbar:false">&#39;;var_dump($data);
<?php/**
 * Express.class.php 快递查询类
 *
 * @author 王浩铭
 * @date 2017/09/27
 */class Express {    /**
     * @desc 采集网页内容的方法,建议使用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;   // 设置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=&#39;&#39;){
        $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 返回的数据结果参考官方文档:https://www.kuaidi100.com/openapi/api_post.shtml
     * @desc 直接调用该方法,传入物流单号即可查询物流信息
     * @param string $order_no
     * @return bool|mixed
     */
    public function getLogisticsInfo($order_no=&#39;&#39;){
        $result = $this->getOrder($order_no);
        $auto_arr = $result[&#39;auto&#39;];        if(count($auto_arr)>0){            foreach ($auto_arr as $key => $value){
                $temp = $this->randFloat();
                $comCode = $value[&#39;comCode&#39;];
                $url = "http://www.kuaidi100.com/query?type=$comCode&postid=$order_no&id=1&valicode=&temp=$temp";// $temp 随机数,防止缓存
                $json = $this->getContent($url);
                $data = json_decode($json,true);                if($data[&#39;message&#39;]==&#39;ok&#39;){                    return $data;
                }
            }
        }        return false;
    }    /**
     * 生成0~1随机小数
     * @param Int  $min
     * @param Int  $max
     * @return Float
     */
    function randFloat($min=0, $max=1){        return $min + mt_rand()/mt_getrandmax() * ($max-$min);
    }
}

本篇介紹了php快遞查詢類別的方法,更多相關知識請關注php中文網。

相關推薦:

php透過curl傳送XML數據,並取得XML數據

PHP完美生成word文檔,可加入html元素

ThinkPhp快取原理及使用詳解

#

以上是關於PHP快遞查詢類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn