查询快递物流

单独对接每个快递公司的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












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

posted @ 2017-02-11 15:58  Soares  阅读(1324)  评论(0)    收藏  举报