forked from jklz/DHL-API-Tracking-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhl_tracking.class.php
290 lines (241 loc) · 9.75 KB
/
dhl_tracking.class.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**********************
Class File
**********************/
class dhl_tracking{
//
var $_PIuserid = "911comprep";
var $_PIpwd = "DiiC08pR3p";
var $_PIurl = "";
var $_PItesturl = "";
var $_PImode = "";
var $_errors = array();
var $errorFail = false;
var $_xml = null;
var $_result = null;
var $_xmlEnd = "\n";
var $checkAuth = false;
var $checkReq = false;
function __construct($mode = 'test'){
//
$this->_PIurl = "https://xmlpi-ea.dhl.com/XMLShippingServlet";
$this->_PItesturl = "https://xmlpitest-ea.dhl.com/XMLShippingServlet";
switch(strtolower($mode)){
case 'live':
// we use live mode
$this->_PImode = "live";
break;
case 'test':
default:
// we default to test mode
$this->_PImode = "test";
break;
}
}
//========================================================================================
// set login info
//========================================================================================
function setAuth($userid = NULL,$pwd = NULL){
if(is_null($userid)){
$this->logError("auth > UserID", $msg = "user id was not set", true);
}else{
$this->_PIuserid = $userid;
}
if(is_null($userid)){
$this->logError("auth > Password", $msg = "Password was not set", true);
}else{
$this->_PIpwd = $pwd;
}
$this->checkAuth
}
function logError($loc = "", $msg = "", $fail = false){
//
$tmp = array(
'location' => $loc,
'message' => $msg,
'stop' => ((bool)$fail ? "Yes" : "No"),
'time' => microtime(true)
);
if((bool)$fail){
$this->errorFail = true;
}
$this->_errors[] = $tmp;
$tmp = NULL;
}
function getErrors(){
//
return ($this->_errors);
}
//========================================================================================
// send pi request
//========================================================================================
function _sendCallPI(){
if(!$ch=curl_init())
{
$this->logError("Send >> Curl", $msg = "Curl is not initialized", true);
return false;
}
else
{
if( $this->checkAuth && $this->checkReq && !$this->errorFail ){
$use_url = ($this->_PImode == "test" ? $this->_PItesturl : $this->_PIurl);
curl_setopt($ch, CURLOPT_URL,$use_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$this->_result = curl_exec($ch);
if(curl_error($ch) != "")
{
$this->logError("Send >> Curl", $msg = "Error with Curl installation: " . curl_error($ch), true);
return false;
}
else
{
curl_close($ch);
return $this->_result;
}
}
}
}
function single($airbill){
//
$this->_xml = "";
$this->_xml .= "<?xml version = '1.0' encoding = 'UTF-8'?>" . $this->_xmlEnd;
$this->_xml .= "<req:KnownTrackingRequest xmlns:req='http://www.dhl.com' ";
$this->_xml .= " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
$this->_xml .= " xsi:schemaLocation='http://www.dhl.com TrackingRequestKnown.xsd'>" . $this->_xmlEnd;
$this->_xml .= "<Request>" . $this->_xmlEnd;
$this->_xml .= "<ServiceHeader>" . $this->_xmlEnd;
$this->_xml .= "<MessageTime>".date("c")."</MessageTime>" . $this->_xmlEnd;
$this->_xml .= "<MessageReference>".time().$airbill."</MessageReference>" . $this->_xmlEnd;
$this->_xml .= "<SiteID>" . $this->_PIuserid . "</SiteID>" . $this->_xmlEnd;
$this->_xml .= "<Password>" . $this->_PIpwd . "</Password>" . $this->_xmlEnd;
$this->_xml .= "</ServiceHeader>" . $this->_xmlEnd;
$this->_xml .= "</Request>" . $this->_xmlEnd;
$this->_xml .= "<LanguageCode>en</LanguageCode>" . $this->_xmlEnd;
$this->_xml .= "<AWBNumber>" . $airbill . "</AWBNumber>" . $this->_xmlEnd;
$this->_xml .= "<LevelOfDetails>ALL_CHECK_POINTS</LevelOfDetails>" . $this->_xmlEnd;
$this->_xml .= "</req:KnownTrackingRequest>" . $this->_xmlEnd;
$abi = simplexml_load_string($this->_sendCallPI());
$tmp_awb = (string)$abi->AWBNumber;
$td['awb'] = $tmp_awb;
$td['res']['status'] = (string)$abi->Status->ActionStatus;
$td['event']['time']['date'] = (string)$abi->ShipmentInfo->ShipmentEvent->Date;
$td['event']['time']['time'] = (string)$abi->ShipmentInfo->ShipmentEvent->Time;
$td['event']['time']['stamp'] = strtotime($td['event']['time']['date'] . " " . $td['event']['time']['time']);
//$td['event']['time']['check'] = date("c", $td['event']['time']['stamp']);
$td['event']['code'] = (string)$abi->ShipmentInfo->ShipmentEvent->ServiceEvent->EventCode;
$tmp_event_desc = (string)$abi->ShipmentInfo->ShipmentEvent->ServiceEvent->Description;
$tmp_event_desc = preg_replace('/\s\s+/', ' ', $tmp_event_desc);
$td['event']['desc'] = $tmp_event_desc;
$tmp_loc_desc = (string)$abi->ShipmentInfo->ShipmentEvent->ServiceArea->Description;
$tmp_loc_desc = preg_replace('/\s\s+/', ' ', $tmp_loc_desc);
$td['event']['location'] = $tmp_loc_desc;
return $td;
}
function multipul($airbill_in = array()){
//
$airbill_in = (array)$airbill_in;
$ab_main = array();
$ab_sub = array();
$ab_out = array();
foreach($airbill_in AS $ab_run){
//
$ab_sub[] = $ab_run;
if(count($ab_sub) == 8){
$ab_main[] = $ab_sub;
$ab_sub = NULL;
$ab_sub = array();
}//END -if(count($ab_sub) == 8){
}//END -foreach($airbill_in AS $ab_run){
if(count($ab_sub) >= 1){
$ab_main[] = $ab_sub;
$ab_sub = NULL;
$ab_sub = array();
}// END -if(count($ab_sub) >= 1){
if(count($ab_main) < 1){
// No Info Passed
$this->logError("Multi >> Count", $msg = "Error with number to track", false);
return false;
} //END - if(count($ab_main) < 1){
foreach($ab_main AS $awb_run){
$track_ab = "";
if(count($awb_run) >= 1 && count($awb_run) <= 10){
foreach($awb_run AS $ab){
$track_ab .= "<AWBNumber>" . $ab . "</AWBNumber>" . $this->_xmlEnd;
}//END -foreach($airbill_in AS $ab){
//now build the output
$ts = time();
$req_reference = $ts . $ts . $ts . $ts . $ts . $ts . $ts . $ts . $ts . $ts;
$req_reference = substr($req_reference, 0, 30);
$req_level = "LAST_CHECK_POINT_ONLY";
//$req_level = "ALL_CHECK_POINTS";
$this->_xml = "";
$this->_xml .= "<?xml version = '1.0' encoding = 'UTF-8'?>" . $this->_xmlEnd;
$this->_xml .= "<req:KnownTrackingRequest xmlns:req='http://www.dhl.com' ";
$this->_xml .= " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
$this->_xml .= " xsi:schemaLocation='http://www.dhl.com TrackingRequestKnown.xsd'>" . $this->_xmlEnd;
$this->_xml .= "<Request>" . $this->_xmlEnd;
$this->_xml .= "<ServiceHeader>" . $this->_xmlEnd;
$this->_xml .= "<MessageTime>".date("c")."</MessageTime>" . $this->_xmlEnd;
$this->_xml .= "<MessageReference>" . $req_reference . "</MessageReference>" . $this->_xmlEnd;
$this->_xml .= "<SiteID>" . $this->_PIuserid . "</SiteID>" . $this->_xmlEnd;
$this->_xml .= "<Password>" . $this->_PIpwd . "</Password>" . $this->_xmlEnd;
$this->_xml .= "</ServiceHeader>" . $this->_xmlEnd;
$this->_xml .= "</Request>" . $this->_xmlEnd;
$this->_xml .= "<LanguageCode>en</LanguageCode>" . $this->_xmlEnd;
// Removed for later possible use
//$this->_xml .= "<AWBNumber>" . $airbill . "</AWBNumber>" . $this->_xmlEnd;
//$this->_xml .= "<AWBNumber></AWBNumber>" . $this->_xmlEnd;
$this->_xml .= $track_ab . $this->_xmlEnd;
$this->_xml .= "<LevelOfDetails>" . $req_level . "</LevelOfDetails>" . $this->_xmlEnd;
$this->_xml .= "</req:KnownTrackingRequest>" . $this->_xmlEnd;
$xml = simplexml_load_string($this->_sendCallPI());
if((string)$xml->Response->Status->ActionStatus == "Failure"){
//is error
$out_error = array(
'req' => $this->_xml,
'res' => $xml
);
$this->logError("Multi >> Responce", $out_error, true);
return false;
}else{ // MID - if((string)$xml->Response->Status->ActionStatus == "Failure")
// is Fine
$tinfo = $xml;
//here we process the responce
if(count($tinfo->AWBInfo) >= 1){
foreach($tinfo->AWBInfo AS $abi){
$tmp_awb = NULL;
$tmp_awb = (string)$abi->AWBNumber;
$td['awb'] = $tmp_awb;
$td['res']['status'] = (string)$abi->Status->ActionStatus;
$td['event']['time']['date'] = (string)$abi->ShipmentInfo->ShipmentEvent->Date;
$td['event']['time']['time'] = (string)$abi->ShipmentInfo->ShipmentEvent->Time;
$td['event']['time']['stamp'] = strtotime($td['event']['time']['date'] . " " . $td['event']['time']['time']);
//$td['event']['time']['check'] = date("c", $td['event']['time']['stamp']);
$td['event']['code'] = (string)$abi->ShipmentInfo->ShipmentEvent->ServiceEvent->EventCode;
$tmp_event_desc = (string)$abi->ShipmentInfo->ShipmentEvent->ServiceEvent->Description;
$tmp_event_desc = preg_replace('/\s\s+/', ' ', $tmp_event_desc);
$td['event']['desc'] = $tmp_event_desc;
$tmp_loc_desc = (string)$abi->ShipmentInfo->ShipmentEvent->ServiceArea->Description;
$tmp_loc_desc = preg_replace('/\s\s+/', ' ', $tmp_loc_desc);
$td['event']['location'] = $tmp_loc_desc;
$ab_out[$tmp_awb] = $td;
$td = null;
}
}
//$ab_out[] = $xml;
}//END - if((string)$xml->Response->Status->ActionStatus == "Failure")
}else{// MID - if(count($airbill_in) >= 1 && count($airbill_in) <= 10){
$this->logError("Send >> Count", "error (if(count($airbill_in) >= 1 && count($airbill_in) <= 10){)", true);
return false;
}// END - if(count($airbill_in) >= 1 && count($airbill_in) <= 10){
}//END -foreach($ab_main AS $awb_run){
return($ab_out);
}//end function
}
?>