关于PHP快递查询类

2022-11-29,,

本篇将会介绍PHP快递查询类的相关方法。

快递公司,只要直接输入快递单号就可以自动识别快递单号所在快递公司和物流信息,还是非常方便的,只要几行代码就可以完美的集成到你系统的功能中了!

使用示例: 使用如下,只需要调用类中的getLogisticsInfo()方法,参数传入订单号即可 $e = new Express();

$data = $e->getLogisticsInfo("453371918456");
echo '<pre>';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=''){
        $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=''){
        $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 随机数,防止缓存
                $json = $this->getContent($url);
                $data = json_decode($json,true);                if($data['message']=='ok'){                    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通过curl发送XML数据,并获取XML数据

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

ThinkPhp缓存原理及使用详解

以上就是关于PHP快递查询类的详细内容,更多请关注北冥有鱼其它相关文章!

本文转载自【PHP中文网】,希望能给您带来帮助,苟日新、日日新、又日新,生命不息,学习不止。

《关于PHP快递查询类.doc》

下载本文的Word格式文档,以方便收藏与打印。