V
V
Vasily2013-12-12 11:47:26
PHP
Vasily, 2013-12-12 11:47:26

How to parse Ukrainian mail?

Good afternoon, until recently I sent a get request to the ip address (unfortunately I can’t say which one, the code on another computer) indicating the track number, the page with the current location of the package was loaded at the output, this page was parsed and entered the necessary data into the system .
I recently discovered that this method does not work and redirects to this page . But I don’t understand how to parse it (there is a lot of incomprehensible data in the POST request, the sending state appears after 3 internal transitions, judging by FireBug).
Maybe they have an adequate API? Or who will prompt as such requests to do?
If something is not clear, please clarify.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2013-12-12
@dedik

Recently, I also discovered this problem on my personal parcel tracker. Some kind of tricky redirect system is now used there.
As a result, I decided easier - there is tracking of tracks from a file (up to 200 tons). I emulate sending a file into which I write all the tracks, and then I use the parser to extract the necessary values ​​from the table.
Here is the code (php, YII):

protected static $apiURL = 'http://services.ukrposhta.com/barcodestatistic/Default.aspx';

    public static function updateTrackingInfo($sendEmail = false) {
        Yii::import('application.lib.simpleHtmlDom.*');

        $packages = Tracking::model()->findAll();
        if (empty($packages)) {
            return;
        }

        $ttns = array();
        $ttnsObj = array();
        foreach ($packages as $package) {
            $ttns[] = $package->tr_num;
            $ttnsObj[$package->tr_num] = $package;
        }

        $result = self::uploadTTNs($ttns);
        
        if(empty($result)){
          return;
        }
        
        $html = new simple_html_dom();
        $html->load($result);
        $rows = $html->find('#ctl00_centerContent_ListBarcodes tr');
        foreach ($rows as $row) {
            $trackNum = trim($row->find('td', 0)->plaintext);
            $package = $ttnsObj[$trackNum];
            if (!empty($trackNum) && !empty($package)) {
                $status = trim($row->find('td', 3)->plaintext);

                if ($status !== $package->last_status) {
                    $package->last_status = $status;
                    $package->last_update = time();
                    $package->notified = 0;
                }
                
                $package->save();
            }
        }        
    }

    public static function uploadTTNs($ttns) {
        if (0 === count($ttns))
            return FALSE;

        $fields = array(
            '__VIEWSTATE' => array(
                'content' => ''
            ),
            'ctl00$centerContent$btnUpload' => array(
                'content' => 'Пошук'
            ),
            'ctl00$centerContent$fileUploadXmlBarcodes' => array(
                'filename' => 'ttn.txt',
                'type' => 'text/plain',
                'content' => implode("\r\n", $ttns)
            ),
        );

        $data = '';
        $delimiter = '-------------' . uniqid();

        foreach ($fields as $name => $field) {
            $data .= "--" . $delimiter . "\r\n";

            $data .= 'Content-Disposition: form-data; name="' . $name . '"';

            if (!empty($field['filename'])) {
                $data.= '; filename="' . $field['filename'] . '"' . "\r\n";
                $data .= 'Content-Type: ' . $field['type'];
            }
            $data .= "\r\n\r\n";

            $data .= $field['content'] . "\r\n";
        }
        $data .= "--" . $delimiter . "--\r\n";

        $handle = curl_init(self::$apiURL);

        $options = array(
            CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: multipart/form-data; boundary=' . $delimiter,
                'Content-Length: ' . strlen($data))
        );
        curl_setopt_array($handle, $options);

        $result = curl_exec($handle);
        curl_close($handle);

        return $result;
    }

D
Dm4k, 2013-12-12
@Dm4k

And you, for your service, or for yourself?
Why not use existing services?
For example, I use this one: post-tracker.ru
, by the way, in the tracker news:

11/16/2013
Ukrainian Post parser fixed

Probably about the same;)
You can try to ask the developer for advice, maybe the person will be adequate)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question