HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/ipsremont-demo/app/Services/Pek/PekService.php
<?php


namespace App\Services\Pek;


use App\Models\ShipmentServerResponse;

class PekService
{
    protected $token;

    public function __construct()
    {
        $this->authorization();
    }

    protected function request($url, $type, $data = null, $shipment_id = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($type == 'POST') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
            curl_setopt($ch, CURLOPT_POST, true);
        } else {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->token,));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);

        $error_code = curl_errno($ch);
        if ($error_code) {
            $err = new ShipmentServerResponse();
            $err->_save($shipment_id, $output, $error_code, curl_error($ch), env('PEK_AUTH_URL'), 'pek');
        }

        curl_close($ch);

        return $output;
    }

    public function authorization()
    {
        $postData = array(//
        );
        $output = $this->request(env('PEK_AUTH_URL'), 'POST', $postData);

        if (isset(json_decode($output)->access_token))
            $this->token = json_decode($output)->access_token;
        else
            $this->token = '';
    }

    public function refresh_token()
    {
        $this->authorization();

        return $this->token;
    }

    public function getOrder($cdek_number, $shipment_id)
    {
        $output = $this->request(env('PEK_GET_ORDER_URL') . $cdek_number, 'GET', '', $shipment_id);

        return json_decode($output);
    }

    public function getStatuses($shipment)
    {
//        $sdk = new PecomKabinet('user', 'FA218354B83DB72D3261FA80BA309D5454ADC');
//
//        $result = $sdk->call('cargos', 'status',
//            array('cargoCodes' => array(
//                'МВПТАМИ-1/1603',
//                'МВПФЗЮН-3/0103',
//                'ОКДВБУТ-4/2903',)
//            ));
//
//        var_dump($result);
//
//        $sdk->close();
////////////////////////////////////////////////////////////
        $ch = curl_init();

        curl_setopt_array($ch, array(
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_POST => TRUE,
            CURLOPT_SSL_VERIFYPEER => TRUE,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_CAINFO => storage_path() . '/cacert-kabinet_pecom_ru.pem',
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
            CURLOPT_ENCODING => 'gzip',
            CURLOPT_USERPWD => 'user:FA218354B83DB72D3261FA80BA309D5454ADC',
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json; charset=utf-8'
            )
        ));

        $request_data = array(
            'cargoCodes' => array(
                'МВПТЗВА-2/2903',
                'МВПЗЗЮН-3/2903',
                'ОКМВБУТ-3/0103'
            ));

        curl_setopt_array($ch, array(
            CURLOPT_URL => 'https://kabinet.pecom.ru/api/v1/cargos/status/',
            CURLOPT_POSTFIELDS => json_encode($request_data),
        ));

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Ошибка: ' . curl_error($ch);
        } else {
            var_dump($result);
            var_dump(json_decode($result));
        }

        curl_close($ch);
    }
}