Home > Backend Development > PHP Tutorial > About PHP express query class

About PHP express query class

jacklove
Release: 2023-03-27 10:38:02
Original
2408 people have browsed it

This article will introduce the related methods of PHP express query class.

Express company, as long as you directly enter the express number, you can automatically identify the express company and logistics information where the express number is located. It is still very convenient. It only takes a few lines of code to be perfectly integrated into the functions of your system. !

Usage example: Use as follows, just call the getLogisticsInfo() method in the class, and pass the order number as the parameter $e = new Express();

$data = $e->getLogisticsInfo("453371918456");
echo &#39;<pre class="brush:php;toolbar:false">&#39;;var_dump($data);
Copy after login
<?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
     */
Copy after login
 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);
    }
}
Copy after login

This article introduces the method of php express query class. For more related knowledge, please pay attention to php Chinese website.

Related recommendations:

php sends XML data through curl and gets XML data

PHP is perfectly generated word document, you can add html elements

ThinkPhp caching principle and usage details

The above is the detailed content of About PHP express query class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template