框架 Yii2
前几天申请的key审核通过了,今天把完整的代码发出来。
<?php
/**
* Created by http://www.kuaidi.com
* User: http://www.kuaidi.com
* Date: 2019/3/28
* Time: 11:04
*/
namespace app\helper;
class KuaiDiAPI
{
private $_APPKEY = '';
private $_APIURL = "http://api.kuaidi.com/openapi.html?";
private $_show = 0;
private $_muti = 0;
private $_order = 'desc';
/**
* 您获得的快递网接口查询KEY。
* @param string $key
*/
public function __construct($key)
{
header("Content-type:text/html;charset=utf-8");
$this->_APPKEY = $key;
}
/**
* 设置数据返回类型。0: 返回 json 字符串; 1:返回 xml 对象
* @param int $show
*/
public function setShow($show = 0)
{
$this->_show = $show;
}
/**
* 设置返回物流信息条目数, 0:返回多行完整的信息; 1:只返回一行信息
* @param int $muti
*/
public function setMuti($muti = 0)
{
$this->_muti = $muti;
}
/**
* 设置返回物流信息排序。desc:按时间由新到旧排列; asc:按时间由旧到新排列
* @param string $order
*/
public function setOrder($order = 'desc')
{
$this->_order = $order;
}
/**
* @param string $nu 物流单号
* @param string $com 公司简码 要查询的快递公司代码,不支持中文,具体请参考快递公司代码文档。 不填默认根据单号自动匹配公司。注:单号匹配成功率高于 95%。
* @return mixed
* @throws \Exception
*/
public function query($nu, $com = '')
{
if (function_exists('curl_init') == 1) {
$url = $this->_APIURL;
$dataArr = array(
'id' => $this->_APPKEY,
'com' => $com,
'nu' => $nu,
'show' => $this->_show,
'muti' => $this->_muti,
'order' => $this->_order
);
foreach ($dataArr as $key => $value) {
$url .= $key . '=' . $value . "&";
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$kuaidresult = curl_exec($curl);
curl_close($curl);
if ($this->_show == 0) {
$result = json_decode($kuaidresult, true);
} else {
$result = $kuaidresult;
}
return $result;
} else {
throw new \Exception("Please install curl plugin",1);
}
}
}
调用:
<?php
$key = '1ee2c0d83f6697942d739e54df942429e1';//自己去官网申请
$kuaidichaxun = new KuaiDiAPI($key);
$nu = '98951263966645';//物流单号
$com = 'youzhengguonei';//公司简码 (见官网文档)
$result = $kuaidichaxun->query($nu,$com);
Logic::vd($result);
Comment here is closed