首页 > 代码库 > php 快递查询api接口笔记

php 快递查询api接口笔记

 1 <?php 
 2 include_once("../includes/global.php");
 3 @include_once("../config/logistics_config.php");
 4 
 5 $api_id = $logistics_config[‘logistic_app_id‘]?$logistics_config[‘logistic_app_id‘]:"";
 6 $api_sceret = $logistics_config[‘logistic_api_sceret‘]?$logistics_config[‘logistic_api_sceret‘]:"";
 7 define("API_Id", $api_id);
 8 define("API_Sceret", $api_sceret); //快递网只需这个 api_id不起作用 http://www.kuaidi.com/
 9 
10 //快递网接口测试--带数据缓存
11 
12 llog(‘‘,true);
15 llog($_GET["com"]);
16 if($_GET["com"]&&$_GET["nu"])
17 {
18     //查询表中支持的快递公司
19     $sql="select * from  ".FASTMAIL." where company=‘".$_GET["com"]."‘";
20     $db->query($sql);
21     if($db->num_rows())
22     {
23         $fast_mail=$db->fetchRow();
24         $com = $fast_mail["pinyin"];
25         $sql2 = "select * from `mail_record` where `number`=‘{$_GET[nu]}‘";
26         llog(‘读取记录-----:‘.$sql2);
27         $res = $db->query($sql2);
28         $info = $db->fetchRow();
29         llog($info);
30         if($info){  //调取缓存
31             if((time()-strtotime($info[‘uptime‘]))>7200){ //超过2小时重新获取
32                 echo $str=kuaidi($com,$_GET["nu"],$info[‘id‘]);
33             }else{
34                 echo "document.write(‘".$info[‘record‘]."‘);";
35             }
36         }else{      //无缓存直接获取
37             echo $str=kuaidi($com,$_GET["nu"]);
38         }
39     }else{
40         echo "document.write(‘暂时没有物流信息!‘);";
41     }
42 }
43 
44 function kuaidi($com,$nu,$id=0){
45 llog(‘==========die;‘);
46 //    return;
47     global $db;
48     $AppKey= API_Sceret?API_Sceret:‘34ba5247777a1bb3d8787700d6add33b‘;  //key自己申请
49     $url =‘http://api.kuaidi.com/openapi.html?id=‘.$AppKey.‘&com=‘.$com.‘&nu=‘.$nu.‘&show=0&muti=0&order=asc‘;
50     llog($url);
51     //优先使用curl模式发送数据
52     if (function_exists(‘curl_init‘) == 1){
53         $curl = curl_init();
54         curl_setopt ($curl, CURLOPT_URL, $url);
55         curl_setopt ($curl, CURLOPT_HEADER,0);
56         curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
57         curl_setopt ($curl, CURLOPT_USERAGENT,$_SERVER[‘HTTP_USER_AGENT‘]);
58         curl_setopt ($curl, CURLOPT_TIMEOUT,5);
59         $con = curl_exec($curl);
60         curl_close ($curl);
61     }
62 
63     llog($con);
64     $con=json_decode($con,true);
65     llog($con);
66 
67     if($con[‘success‘] && count($con[‘data‘])>0){
68         $tables= ‘<table width="520px" border="0" cellspacing="0" cellpadding="0" id="showtablecontext" style="border-collapse:collapse;border-spacing:0;"><tbody><tr><td width="163" style="background:#39b54a;border:1px solid #39b54a;color:#FFFFFF;font-size:14px;font-weight:bold;height:28px;line-height:28px;padding:3px 5px;">时间</td><td width="354" style="background:#39b54a;border:1px solid #39b54a;color:#FFFFFF;font-size:14px;font-weight:bold;height:28px;line-height:28px;padding:3px 5px;">地点和跟踪进度</td></tr>‘;
69         foreach($con[‘data‘] as $vo){
70             $tables.=‘<tr><td width="163" style="border:1px solid #DDDDDD;font-size:12px;line-height:22px;padding:3px 5px;">‘.$vo[‘time‘].‘</td><td width="354" style="border:1px solid #DDDDDD;font-size:12px;line-height:22px;padding:3px 5px;text-align: left;">‘.$vo[‘context‘].‘</td></tr>‘;
71         }
72         $tables.= ‘</tbody></table>‘;
73     }elseif(isset($con[‘reason‘]) && strlen($con[‘reason‘])>0 ){
74 
75        return "document.write(‘<p style=\‘color:red;font-size:14px;font-weight:bold;\‘>" . $con[‘reason‘] ."!</p>‘);";
76     }
77     llog($tables);
78     if($id){
79         $sql = "update `mail_record` set `record`=‘{$tables}‘ where id=‘{$id}‘";
80 
81     }else{
82         $sql = "insert into `mail_record` (`code`,`number`,`record`) VALUE (‘{$com}‘,‘{$nu}‘,‘{$tables}‘)";
83     }
84     $db->query($sql);
85     llog(‘插入----:‘.$sql);
86 
87     return "document.write(‘" . $tables ."‘);";
88 }
89 
90 
91 ?>

下面是快递100和爱查快递api接口

 1 /** 快递100 https://www.kuaidi100.com/openapi/api_post.shtml
 2  * @param $com 快递公司代码,不支持中文
 3  * @param $nu 快递单号,请勿带特殊符号,不支持中文(大小写不敏感)
 4  * @param $show 返回类型:如果不填,默认返回json字符串
 5                 0:返回json字符串,
 6                 1:返回xml对象,
 7                 2:返回html对象,
 8                 3:返回text文本。
 9  * @return string
10  */
11 function php100($com,$nu){
12     $AppKey= API_Sceret?API_Sceret:‘a704e4fb5fcc8124‘;//请将XXXXXX替换成您在http://kuaidi100.com/app/reg.html申请到的KEY
13     $url =‘http://api.kuaidi100.com/api?id=‘.$AppKey.‘&com=‘.$com.‘&nu=‘.$nu.‘&show=2&muti=1&order=asc‘;
14     //请勿删除变量$powered 的信息,否则本站将不再为你提供快递接口服务。
15     $powered = ‘查询数据由:<a href=http://www.mamicode.com/"http://kuaidi100.com\" target=\"_blank\">KuaiDi100.Com (快递100)</a> 网站提供 ‘;
16     //优先使用curl模式发送数据
17     if (function_exists(‘curl_init‘) == 1){
18         $curl = curl_init();
19         curl_setopt ($curl, CURLOPT_URL, $url);
20         curl_setopt ($curl, CURLOPT_HEADER,0);
21         curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
22         curl_setopt ($curl, CURLOPT_USERAGENT,$_SERVER[‘HTTP_USER_AGENT‘]);
23         curl_setopt ($curl, CURLOPT_TIMEOUT,5);
24         $con = curl_exec($curl);
25         curl_close ($curl);
26     }
27 
28     return ‘document.write("‘ . $con .‘<br/>‘ . $powered.‘");‘;
29 }
30 
31 
32 
33 // 爱查快递 http://api.ickd.cn/?id=[]&secret=[]&com=[]&nu=[]&type=[]&encode=[]&ord=[]&lang=[]
34 /*com  必须 快递公司代码(英文),所支持快递公司见如下列表
35 nu 必须 快递单号,长度必须大于5位
36 id 必须 授权KEY,申请请点击快递查询API申请方式
37 在新版中ID为一个纯数字型,此时必须添加参数secret(secret为一个小写的字符串)
38 secret 必选(新增) 该参数为新增加,老用户可以使用申请时填写的邮箱和接收到的KEY值登录http://api.ickd.cn/users/查看对应secret值
39 type   可选 返回结果类型,值分别为 html | json(默认) | text | xml
40 encode 可选 gbk(默认)| utf8
41 ord    可选 asc(默认)|desc,返回结果排序
42 lang   可选 en返回英文结果,目前仅支持部分快递(EMS、顺丰、DHL)*/
43 function lookorder($com,$nu,$type)
44 {
45     $api_id = API_Id;
46     $api_sceret = API_Sceret;
47     $url2 = "http://api.ickd.cn/?com=" . $com . "&nu=" . trim($nu) . "&id=" . $api_id . "&type=" . $type . "&secret=" . $api_sceret . "&encode=utf8";
48     $con = file_get_contents($url2);
49     if ($type == "json") {
50         return $con;
51     } else {
52         return "document.write(‘" . $con . "‘);";
53     }
54 }

前台调用:

<{if $de.logistics_name&&$de.invoice_no&&$logistics_config.logistics_connect==1}>
<dl><script src="http://www.mamicode.com/<{$config.weburl}>/api/logistic3.php?com=<{$de.logistics_name}>&nu=<{$de.invoice_no}>"></script></dl>
<{/if}>

 

php 快递查询api接口笔记