KdBrod.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace app\api\controller;
use think\Db;
use app\common\controller\Api;
//调用查询物流轨迹
//---------------------------------------------
/**
* 快递接口
*/
class KdBrod extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/**
* @ApiTitle (快递接口-查询)
* @ApiSummary (查询)
* @ApiMethod (POST)
* @ApiRoute (/api/Kd_Brod/kuaidi)
* @ApiParams (name="shop", type="string", required=true, description="快递名称")
* @ApiParams (name="shop_order", type="string", required=true, description="快递单号")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
"LogisticCode": "75370537792337",
"ShipperCode": "中通快递",
"Traces": [
{
"AcceptStation": "【广州鸿顺】(020-22046523) 的 温建阳(19927413363) 已揽收",
"AcceptTime": "2020-07-15 16:03:05"
},
{
"AcceptStation": "快件离开 【广州鸿顺】 已发往 【天津中转部】",
"AcceptTime": "2020-07-16 04:51:28"
},
{
"AcceptStation": "快件已经到达 【广州中心】",
"AcceptTime": "2020-07-16 08:44:19"
},
{
"AcceptStation": "快件离开 【广州中心】 已发往 【天津中转部】",
"AcceptTime": "2020-07-16 08:54:04"
},
{
"AcceptStation": "快件已经到达 【天津中转部】",
"AcceptTime": "2020-07-17 22:09:53"
},
{
"AcceptStation": "快件离开 【天津中转部】 已发往 【天津海泰分部】",
"AcceptTime": "2020-07-17 22:10:02"
},
{
"AcceptStation": "快件已经到达 【天津海泰分部】",
"AcceptTime": "2020-07-18 06:38:50"
},
{
"AcceptStation": "【天津海泰分部】 的吴可可(18622722466) 正在第1次派件, 请保持电话畅通,并耐心等待(95720为中通快递员外呼专属号码,请放心接听)",
"AcceptTime": "2020-07-18 07:34:56"
},
{
"AcceptStation": "您的快递已暂存至【菜鸟的天津格调松间北里配建3】。如有问题请电联:(18622722466), 投诉电话:(9519666), 感谢您使用中通快递, 期待再次为您服务!",
"AcceptTime": "2020-07-18 10:03:05"
}
],
"State": "2",
"EBusinessID": "1649160",
"Success": true
}
})
*/
public function kuaidi()
{
$param = $this->request->param();
$code = Db::name('kuaidicode')->where(['kuaidi' => $param['shop']])->value('code');
$logisticResult = $this->getOrderTracesByJson($code, $param['shop_order']);
$kuaidi = json_decode(htmlspecialchars_decode($logisticResult), true);
$shopname = Db::name('kuaidicode')->where(['code' => $kuaidi['ShipperCode']])->value('kuaidi');
$return['LogisticCode'] = $kuaidi['LogisticCode'];
$return['ShipperCode'] = $shopname;
$return['Traces'] = $kuaidi['Traces'];
// $return['State'] = $kuaidi['State'];
// $return['EBusinessID'] = $kuaidi['EBusinessID'];
// $return['Success'] = $kuaidi['Success'];
$this->success('成功', $return);
}
//---------------------------------------------
/**
* Json方式 查询订单物流轨迹
*/
function getOrderTracesByJson($kgs, $number)
{
$requestData = "{'OrderCode':'','ShipperCode':'$kgs','LogisticCode':'$number'}";
$datas = array(
'EBusinessID' => '1649160',
'RequestType' => '1002',
'RequestData' => urlencode($requestData),
'DataType' => '2',
);
$datas['DataSign'] = $this->encrypt($requestData, 'fdf75944-8e72-4c72-b40f-534b0336b552');
$result = $this->sendPost('http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
//根据公司业务处理返回的信息......
return $result;
}
/**
* post提交数据
* @param string $url 请求Url
* @param array $datas 提交的数据
* @return url响应返回的html
*/
function sendPost($url, $datas)
{
$temps = array();
foreach ($datas as $key => $value) {
$temps[] = sprintf('%s=%s', $key, $value);
}
$post_data = implode('&', $temps);
$url_info = parse_url($url);
if (empty($url_info['port'])) {
$url_info['port'] = 80;
}
$httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
$httpheader .= "Host:" . $url_info['host'] . "\r\n";
$httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
$httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
$httpheader .= "Connection:close\r\n\r\n";
$httpheader .= $post_data;
$fd = fsockopen($url_info['host'], $url_info['port']);
fwrite($fd, $httpheader);
$gets = "";
$headerFlag = true;
while (!feof($fd)) {
if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
break;
}
}
while (!feof($fd)) {
$gets .= fread($fd, 128);
}
fclose($fd);
return $gets;
}
/**
* 电商Sign签名生成
* @param data 内容
* @param appkey Appkey
* @return DataSign签名
*/
function encrypt($data, $appkey)
{
return urlencode(base64_encode(md5($data . $appkey)));
}
}
?>