-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rastreio.php
executable file
·147 lines (121 loc) · 4.02 KB
/
Rastreio.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
<?php
class CorreiosTracking {
const DATA_EMPTY = 0;
const INFO_EMPTY = "No info";
const MESSAGE_EMPTY = "Our system cannot find any information about this package. If it has been recently posted is normal it not appear so soon, please try again latter,, otherwise check if the given code are correctly typed";
//parms: ?P_LINGUA=001&P_TIPO=001&P_COD_UNI=
static public $searchUrl = "http://websro.correios.com.br/sro_bin/txect01$.QueryList";
/**
* Track an object
*
* @param String $identify
*/
public function trackItem($identify) {
$object = new CorreiosTrackingItem($identify);
$this->_getItemHistory($object);
return $object;
}
/**
* It creates a new CorreiosTrackingItem object based on the identify parm (DEPRECATED)
*
* @param String $identify
* @param Integer $language
* @param Integer $type
* @return CorreiosTrackingItem
*/
public function newObject($identify, $language = 001, $type = 001) {
return new CorreiosTrackingItem($identify, $language, $type);
}
/**
* It retreives the history of a range of items
* TODO Use a less band usage method as for example parse based on the multiple resource
* @param array $objects
*/
public function getFromArray(array &$objects) {
foreach($objects as $index => $item) {
$this->_getItemHistory($item);
}
}
/**
* PROTECTED AND PRIVATE FUNCTIONS
*/
/**
* It gets the tracking history of a object at Correios
*
* @param CorreiosTrackingItem $object
*/
final private function _getItemHistory(CorreiosTrackingItem &$object) {
$sentVars['P_LINGUA'] = $object->language;
$sentVars['P_TIPO'] = $object->type;
$sentVars['P_COD_UNI'] = $object->identify;
//Creating the URL
$url = self::$searchUrl . "?" . http_build_query($sentVars);
$ch = curl_init();
$timeout = 0; //zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$finalFile = array();
$finalFile = explode("\n", $file_contents);
$object->tracking = $this->parseContent($finalFile);
}
protected function _parseDOMContent($html) {
//Searching using MAIN STRUCTURE OF THE TABLE
$tableOfContents = $html->find('table');
foreach($tableOfContents->children() as $index => $tr) {
if($index > 0) {
foreach($tr->find('td') as $trIndex => $td) {
print $td->innertext;
}
}
}
}
/**
* This function parses the retreived content DEPRECATED
*
* @param String $content
* @return Array
*/
protected function parseContent($content) {
$items = Array();
/*
* It starts the replacement of the retreived data
* If there any changes in the retreived HTML it have to be changed
* TODO Alwasy check if the HTML corresponds to this patterns
*/
foreach ($content as $num => $line) {
if (substr($line, 0, 7) == '<tr><td') {
if (preg_match('/<td rowspan=[0-9]>.+?<\/td>/', $line, $match))
$items[$num]['data'] = utf8_encode(strip_tags($match[0]));
if (preg_match('/<td colspan=[0-9]>.+?<\/td>/', $line, $match))
$items[$num-1]['to'] = utf8_encode(strip_tags($match[0]));
if (preg_match('/<td>.+?<\/td>/', $line, $match))
$items[$num]['message'] = utf8_encode(strip_tags($match[0]));
if (preg_match('/<FONT.*>.+?<\/font>/', $line, $match))
$items[$num]['info'] = utf8_encode(strip_tags($match[0]));
}
}
if (!$items) {
$items[0]['data'] = self::DATA_EMPTY;
$items[0]['infos'] = self::INFO_EMPTY;
$items[0]['message'] = self::MESSAGE_EMPTY;
}
return $items;
}
}
class CorreiosTrackingItem {
//By default its Brazilian Portuguese but you can use 002 as english but the support isn't full
public $language = "001";
//CONSTANT DO NOT CHANGE IT
public $type = "001";
//Your track number
public $identify;
public $tracking = Array();
final public function __construct($identify, $language = "001", $type = "001") {
$this->identify = $identify;
$this->language = $language;
$this->type = $type;
}
}