Files
liche/common/libraries/AliWuliu.php
T
2023-02-20 12:13:55 +08:00

126 lines
5.5 KiB
PHP

<?php
/**
* Desc: 阿里云 全国快递物流查询
* Date: 2023-02-20
*/
defined('BASEPATH') OR exit('No direct script access allowed');
error_reporting(E_ALL || ~E_NOTICE);
class AliWuliu{
private $appkey;
private $appcode;
private $appecret;
private $ci;
private $dir;
private $success_file = 'success.log';
private $log_file = 'error.log';
public function __construct($appcode=''){
$this->appcode = $appcode ? $appcode : 'e9f05c2fdf65458a80e8619141481cee';
$this->ci = & get_instance();
$this->dir = 'aliwuliu';
}
/**
* Desc: https://market.aliyun.com/products/57126001/cmapi021863.html?spm=5176.2020520132.101.4.2de27218yEmTb1#sku=yuncode1586300000
* Desc: https://netmarket.oss.aliyuncs.com/e885884a-8666-4d9d-85a9-692250a5510a.pdf?spm=5176.2020520132.101.6.2de27218yEmTb1&file=e885884a-8666-4d9d-85a9-692250a5510a.pdf
* @param $no string 快递单号 【顺丰和丰网请输入单号 : 收件人或寄件人手机号后四位。例如:123456789:1234
* @param $type string 快递公司字母简写:不知道可不填 95%能自动识别,填写查询速度会更快
* @param $debug bool
* @return array 返回值参考官方文档
*/
public function kdi($no, $type='', $debug=false){
if (!$no){
return array('status'=>201, 'msg'=>'快递单号错误','result'=>array('number'=>'','type'=>'AUTO'),'code'=>0);
}
$host = "https://wuliu.market.alicloudapi.com";//api访问链接
$path = "/kdi";//API访问后缀
$method = "GET";
$appcode = $this->appcode;//开通服务后 买家中心-查看AppCode
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
#$querys = "no=780098068058&type=zto"; //参数写在这里
$bodys = "";
$querys = $type ? "no={$no}&type={$type}" : "no={$no}";
$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, true);
if (1 == strpos("$" . $host, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$out_put = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
list($header, $body) = explode("\r\n\r\n", $out_put, 2);
if ($httpCode == 200) {
$debug && print("正常请求计费(其他均不计费)<br>");
$debug && print($body);
$data = array(
'no' =>$no,
'type' =>$type,
'httpCode' => $httpCode,
'msg' => $body,
);
debug_log(json_encode($data,JSON_UNESCAPED_UNICODE),$this->success_file,$this->dir);
$res = json_decode($body, true);
$res['code'] = 1;
return $res;
} else {
if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
$msg = '参数错误';
$debug && print("参数错误");
} elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
$msg = 'AppCode错误';
$debug && print("AppCode错误");
} elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
$msg = '请求的 Method、Path 或者环境错误';
$debug && print("请求的 Method、Path 或者环境错误");
} elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
$msg = '服务未被授权(或URL和Path不正确)';
$debug && print("服务未被授权(或URL和Path不正确)");
} elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
$msg = '套餐包次数用完';
$debug && print("套餐包次数用完");
} elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
$msg = '套餐包次数用完,请续购套餐';
$debug && print("套餐包次数用完,请续购套餐");
} elseif ($httpCode == 500) {
$msg = 'API网关错误';
$debug && print("API网关错误");
} elseif ($httpCode == 0) {
$msg = 'URL错误';
$debug && print("URL错误");
} else {
$msg = '参数名错误 或 其他错误';
$debug && print("参数名错误 或 其他错误");
$debug && print($httpCode);
$headers = explode("\r\n", $header);
$headList = array();
foreach ($headers as $head) {
$value = explode(':', $head);
$headList[$value[0]] = $value[1];
}
$debug && print($headList['x-ca-error-message']);
}
$data = array(
'no' =>$no,
'type' =>$type,
'httpCode' => $httpCode,
'msg' => $msg,
);
debug_log(json_encode($data,JSON_UNESCAPED_UNICODE),$this->log_file,$this->dir);
return false;
}
}
}