使用curl()模拟请求,实现快递查询

首先php内容为

<?php

    include "function.php";

    //$code 就是你要查询的快递单号
    $code = "70842272046150";

    //$kurl是获取快递公司的接口
    $kurl = 'http://www.kuaidi100.com/autonumber/autoComNum?text='.$code;

    $company = get_json_decode('',$kurl);

    foreach ($company['auto'] as $k => $v) {

        $com = $v['comCode'];

        //$url是获取物流详情接口
        $url = 'https://www.kuaidi100.com/query?type='.$com.'&postid='.$code.'&id=1&valicode=&temp=0.2925338583829262';

        $companys = get($url);

        if ($companys) {

            $res = json_decode($companys,true);

            //var_dump($res['data']);
            foreach ($res['data'] as $key => $value) {
                if (isset($value['time']) && $value['time']) {
                    var_dump($value);
                }
            }
            return;
        }

    }

导入的function文件内容为

<?php
    function get_json_decode($params = '', $mybaseurl ='www.baidu.com')
        {
            $ch = curl_init();// 初始化curl
            curl_setopt($ch, CURLOPT_URL,$mybaseurl);  
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      
            // 执行请求
            $result = curl_exec($ch);
            $errno = curl_errno($ch);
            $http_code = curl_getinfo($ch);

            if($result == null){
                $data['result'] = false;
                $data['errCode'] = '发生错误';
                curl_close($ch);
                // 失败返回空数组
                return $data;
            }
            // 取得返回的结果,转换成对象
            $data = json_decode($result, true);
            //print_r($data);
            // 关闭CURL
            curl_close($ch);
            return $data;
        }

猜你喜欢

转载自blog.csdn.net/u011140030/article/details/53410494