• 查询快递物流


    单独对接每个快递公司的api会比较麻烦,一般都选择第三方来对接
    服务来源: 阿里云 付费(0.01元100次) 购买服务后,商家提供AppKey、AppSecret、AppCode

    购买物流查询接口api的服务,到官网上能找到各种程序语言对应的demo,稍微修改下就能正常使用.重温下curl以及ajax的使用.

    logistics.php

    $host = "http://ali-deliver.showapi.com";
    $path = "/showapi_expInfo";
    $method = "GET";
    $appcode = "你的APPCODE";
    $headers = array();
    
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "com=auto&nu=".$_POST['nu'];  //设置com为auto自动识别快递公司,nu是前台输入的快递单号
    $bodys = "";
    $url = $host . $path . "?" . $querys;
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $return = curl_exec($curl);
    
    echo $return;
    

    用php的CURL库模拟浏览器动作把请求的url和header传到服务器,执行后返回快递物流的详细信息,在前端显示

    logistics.html

    <html>
    <head>
        <script src="./jquery-3.1.1.min.js"></script>
        <script type="text/javascript">
            function loadXMLDoc()
            {
                $.ajax({
                    type:'POST',
                    url:'./test.php',
                    data:{
                        nu:$('#nu').val()
                    },
                    dataType:'json',
                    success:function(result){
                        console.log(result);
                        if(result.showapi_res_code == 0){
                            var data = result.showapi_res_body;
                            var content = '快递公司:'+data.expTextName+'<br><br><hr>物流详情:<br>';
                            for(var i=0;i<data.data.length;i++){
                                content += data.data[i].time+'<br>'+data.data[i].context+'<br><br>';
                            }
                        }else{
                            var content = result.showapi_res_error;
                        }
                        $('#wuliu').html(content);
                    }
                });
            }
        </script>
    </head>
    <body>
    
    <input type="text" id="nu" value=""/>
    <button type="button" onclick="loadXMLDoc()">查询单号</button>
    <br>
    <div id="wuliu" style="margin-top: 20px;">
    </div>
    
    </body>
    </html>
    

    前台是用ajax访问php文件,返回json数据(物流详情)

  • 相关阅读:
    解题报告 poj 1486
    解题报告 比赛
    解题报告 keke 的房子
    解题报告 Tree
    解题报告 聚会
    解题报告 Valentine‘s seat
    解题报告 报数
    解题报告 黑书 Water pail poi 1999
    解题报告 poj 1639
    解题报告 数数
  • 原文地址:https://www.cnblogs.com/shihua/p/6389179.html
Copyright © 2020-2023  润新知