• 技术文章 >后端开发 >php教程

    关于PHP快递查询类

    jacklovejacklove2018-05-22 09:30:17原创1471
    本篇将会介绍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中文网。

    相关推荐:

    php通过curl发送XML数据,并获取XML数据

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

    ThinkPhp缓存原理及使用详解

    php入门到就业线上直播课:查看学习

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

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php 查询 快递
    上一篇:php实现获取数据库结果集的方法 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 你知道如何用PHP实现多进程吗• PHP与MySQL连接的方法总结• 工具包分享:PHP实现滑块验证图片• php实现mysql数据库分表分段备份_php实例• 找到一个编辑器,但是不知道来得到里面的值!求解解决方案
    1/1

    PHP中文网