amka.php 2.78 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
<?php

$settings = require(__DIR__ . '/settings.php');

/**
 * Λήψη παραμέτρων καθορισμού λειτουργίας από τη γραμμή εντολών 
 */
$options = getopt('v', ['amka:', 'surname:']);

$amka = isset($options['amka']) ? $options['amka'] : '';
$surname = isset($options['surname']) ? $options['surname'] : '';
$bdate = '';
if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})[0-9]*/', $amka, $bdate_parts) === 1) {
    $bdate = "{$bdate_parts[3]}-{$bdate_parts[2]}-{$bdate_parts[1]}";
    if (intval($bdate_parts[3]) > 40) {
        $bdate = "19{$bdate}";
    }
}

/**
 * Έλεγχος παραμέτρων
 */
if ($amka == '' || $surname == '' || $bdate == '') {
    echo "Χρήση: {$argv[0]} [-v] --amka <amka> --surname <surname>", PHP_EOL,
    "                v: παραγωγή μηνυμάτων παρακολούθησης εκτέλεσης", PHP_EOL,
    "      amka <amka>: ο αριθμός ΑΜΚΑ", PHP_EOL,
27
    "surname <surname>: επίθετο σε ΚΕΦΑΛΑΙΑ", PHP_EOL;
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
    exit(0);
}

/**
 * Παράμετροι αποστολής
 */
$data = [
    'ssn' => $amka,
    'birthdate' => $bdate,
    'surname' => $surname,
];
$url = $settings['url'] . '?' . http_build_query($data);

if (isset($settings['extra_headers']) && is_array($extra_headers = $settings['extra_headers'])) {
    $headers = array_reduce(array_keys($extra_headers), function ($h, $key) use ($extra_headers) {
        array_push($h, "{$key}: {$extra_headers[$key]}");
        return $h;
    }, []);
}

// Setup cURL
$ch = curl_init($settings['url']);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
    "Authorization: Token {$settings['key']}",
    'Content-Type: application/json'
], $headers));
curl_setopt($ch, CURLOPT_USERAGENT, "OSTEAM barebone php client");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_VERBOSE, isset($options['v']));

if (isset($settings['verify_ssl']) && $settings['verify_ssl'] === false) {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}

$result = curl_exec($ch);

if (curl_errno($ch)) {
    die("Λάθος κατά την κλήση του {$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) {
    die("Αποτυχημένη κλήση. HTTP STATUS {$http_code}. Η απάντηση ήταν: {$result}");
}
curl_close($ch);

$responseData = json_decode($result, TRUE);
array_walk($responseData, function ($v, $k) {
    echo "{$k}: {$v}" . PHP_EOL;
});
// ή απλά print_r($responseData);