Client.php 6.73 KB
Newer Older
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
<?php

/*
 * 
 */

namespace Gr\Gov\Minedu\Osteam\App;

use Exception;

/**
 * Description of Client
 *
 * @author spapad
 */
class Client
{

    private $_debug = false;
    private $_settings = [
        'base_uri' => '',
        'username' => '',
        'password' => '',
        'NO_SAFE_CURL' => true
    ];

    public function __construct($settings = [])
    {
        $this->_settings = array_merge($this->_settings, $settings);
    }

    public function check($amka, $auth) {
        $data = [
        ];
        $headers = [
            'Authorization: ' . $auth,
            // 'Accept: */*',
        ];

        $results = $this->get($this->_settings['base_uri'] . "/student/{$amka}", $data, $headers);
        // echo " CHECK RESULT IS: ", var_export($results, true), PHP_EOL;
        if ($results['success'] === false) {
                return false;
        }

        return $results['response'];
    }

    public function rawcheck($amka) {
        $pass_md5 = md5($this->_settings['password']);
        $auth = "Basic " . base64_encode("{$this->_settings['username']}:{$pass_md5}");

        $data = json_encode([
            "AMKA" => $amka
        ]);

        $headers = [
            "Authorization: {$auth}",
            'Content-Type: application/json',
            'Accept: */*',
        ];

63
        $results = $this->post($this->_settings['raw_check_base_uri'], $data, $headers);
64 65 66 67 68 69 70 71 72 73 74 75 76
        echo " RAWCHECK RESULT IS: ", var_export($results, true), PHP_EOL;
        if ($results['success'] === false) {
            return false;
        }

        $parsed_result = json_decode($results['response'], true);
        if ($parsed_result === false) {
            return false;
        }
        if ($parsed_result['response'] === 'SUCCESS' && $parsed_result['errorReason'] === null) {
            if (isset($parsed_result['inspectionResult']) 
                && $parsed_result['inspectionResult']['webServiceSuccess'] === true
                && is_array($parsed_result['inspectionResult']['beneficiaryNames'])) {
77 78 79 80 81 82
                if (count($parsed_result['inspectionResult']['beneficiaryNames']) > 0) {
                    if (is_array($bNames = $parsed_result['inspectionResult']['beneficiaryNames'][0]) && 
                        isset($bNames['beneficiaryName'])) {
                        return $bNames['beneficiaryName'];
                    }
                }
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
            }
        }
        return false;
    }

    protected function setCommonCurlOptions($ch, $uri, $headers)
    {
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_USERAGENT, "OSTEAM client");
        if (isset($this->_settings['NO_SAFE_CURL']) && $this->_settings['NO_SAFE_CURL'] === true) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        if ($this->_debug === true) {
            curl_setopt($ch, CURLOPT_VERBOSE, true);
        }
    }

    public function put($uri, $payload, $headers = [])
    {
        $ch = curl_init();

        $this->setCommonCurlOptions($ch, $uri, $headers);

        // curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception("Λάθος κατά την κλήση του {$uri}. Curl error: " . curl_error($ch) . " Curl info: " . var_export(curl_getinfo($ch), true));
        }
        if (intval(($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) / 100) != 2) {
        // πραγματοποιήθηκε κλήση αλλά δεν ήταν "επιτυχής"
            throw new Exception("Αποτυχημένη κλήση. HTTP STATUS {$http_code}. Η απάντηση ήταν: {$result}", $http_code);
        }
        curl_close($ch);
        return $result;
    }

    public function post($uri, $payload, $headers = [])
    {
        $ch = curl_init();

        $this->setCommonCurlOptions($ch, $uri, $headers);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception("Λάθος κατά την κλήση του {$uri}. Curl error: " . curl_error($ch) . " Curl info: " . var_export(curl_getinfo($ch), true));
        }
        if (intval(($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) / 100) != 2) {
        // πραγματοποιήθηκε κλήση αλλά δεν ήταν "επιτυχής"
            return [
                'success' => false,
                'http_status' => $http_code,
                'response' => $result
            ];
            // throw new Exception("Αποτυχημένη κλήση. HTTP STATUS {$http_code}. Η απάντηση ήταν: {$result}", $http_code);
        }
        curl_close($ch);
        return [
            'success' => true,
            'http_status' => $http_code,
            'response' => $result
        ];
    }

    public function get($uri, $params = [], $headers = [])
    {
        $ch = curl_init();

        if (is_array($params) && count($params) > 0) {
            $qs = '?' . http_build_query($params);
        } else {
            $qs = '';
        }
        $this->setCommonCurlOptions($ch, "{$uri}{$qs}", $headers);

//        curl_setopt($ch, CURLOPT_HTTPGET, true); // default
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception("Λάθος κατά την κλήση του {$uri}. Curl error: " . curl_error($ch) . " Curl info: " . var_export(curl_getinfo($ch), true));
        }
        if (intval(($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) / 100) != 2) {
        // πραγματοποιήθηκε κλήση αλλά δεν ήταν "επιτυχής"
            return [
                'success' => false,
                'http_status' => $http_code,
                'response' => $result
            ];
            // throw new Exception("Αποτυχημένη κλήση. HTTP STATUS {$http_code}. Η απάντηση ήταν: {$result}", $http_code);
        }
        curl_close($ch);
        return [
            'success' => true,
            'http_status' => $http_code,
            'response' => $result
        ];
    }

    public function setDebug($debug = true)
    {
        $this->_debug = ($debug === true);
        return;
    }

}