This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracking_pochta.php
140 lines (116 loc) · 4.27 KB
/
tracking_pochta.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
<?php
/**
* @author Odiva.ru
* Date: 12.11.15
* request for unlimited access: https://tracking.pochta.ru/request
* @link https://tracking.pochta.ru/specification
*/
ini_set('default_socket_timeout', 15);
class PochtaApi
{
/** @link https://tracking.pochta.ru/access-settings */
private $login = 'login';
private $pass = 'pass';
private $singleHost = 'https://tracking.russianpost.ru/rtm34?wsdl';
private $ticketHost = 'https://tracking.russianpost.ru/fc?wsdl';
private $ticketDataFile = '/data/ticket.txt';
private $errorLogFile = '/data/error_log.txt';
private $needTrace = true;
const CLIENT_TYPE_SINGLE = 1;
const CLIENT_TYPE_TICKET = 2;
private function _objectToArray($o)
{
return json_decode(json_encode($o), true);
}
private function _call($method, $params = array(), $clientType = self::CLIENT_TYPE_SINGLE)
{
$params = $params ? $params : array();
$authParams = array('login' => $this->login, 'password' => $this->pass);
if ($clientType == self::CLIENT_TYPE_TICKET) {
list ($host, $ver) = array($this->ticketHost, SOAP_1_1);
$params = array_merge($params, $authParams);
} else {
list ($host, $ver) = array($this->singleHost, SOAP_1_2);
$params['AuthorizationHeader'] = $authParams;
}
$client = new SoapClient($host, array('soap_version' => $ver, 'encoding' => 'UTF-8', 'trace' => $this->needTrace, 'connection_timeout' => 5));
try {
$response = $client->$method($params);
if ($response->error) {
throw new SoapFault("Message", $response->error->ErrorName);
}
return $this->_objectToArray($response);
} catch (SoapFault $ex) {
$this->_exceptionLog($ex);
return false;
}
}
private function _exceptionLog($ex)
{
$this->_file_force_contents(__DIR__ . $this->errorLogFile, date("Y-m-d H:i:s") . '; ' . $ex->getMessage() . '; in Line: ' . $ex->getLine() . "\r\n");
}
private function _file_force_contents($dir, $contents, $flags = FILE_APPEND)
{
$parts = explode('/', $dir);
$file = array_pop($parts);
$dir = '';
foreach ($parts as $part) {
if (!is_dir($dir .= "/$part")) mkdir($dir);
}
return file_put_contents("$dir/$file", $contents, $flags);
}
public function formatTrack($track)
{
$track = trim($track);
try {
if (!preg_match('/^[0-9]{14}|[A-Z]{2}[0-9]{9}[A-Z]{2}$/', $track)) {
throw new SoapFault("Message", 'Некорректный формат почтового идентификатора: ' . $track);
}
return $track;
} catch (SoapFault $ex) {
$this->_exceptionLog($ex);
return false;
}
}
public function getTicket($arTracks)
{
$params = array();
foreach ($arTracks as $key => $track) {
if ($track = $this->formatTrack($track)) {
$params['request']['Item'][$key]['Barcode'] = $track;
}
}
$response = $this->_call('getTicket', $params, self::CLIENT_TYPE_TICKET);
$ticketId = $response['value'];
$this->writeTicketId($ticketId);
return $ticketId;
}
public function clearTicketId()
{
$this->writeTicketId('');
}
public function writeTicketId($ticketId)
{
$flags = 0;
return $this->_file_force_contents(__DIR__ . $this->ticketDataFile, $ticketId, $flags);
}
public function readTicketId()
{
return file_get_contents(__DIR__ . $this->ticketDataFile);
}
public function getOperationHistory($track)
{
if ($track = $this->formatTrack($track)) {
$requestParams['OperationHistoryRequest']['Barcode'] = $track;
$requestParams['OperationHistoryRequest']['MessageType'] = '0';
return $this->_call('getOperationHistory', $requestParams, self::CLIENT_TYPE_SINGLE);
}
return false;
}
public function getResponseByTicket($ticket)
{
$params['ticket'] = $ticket ? $ticket : $this->readTicketId();
return $this->_call('getResponseByTicket', $params, self::CLIENT_TYPE_TICKET);
}
}
?>