Command line client

parent 216c697e
<?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,
"surname <surname>: επίθετο σε ΚΕΦΑΛΑΙΑ", PHP_EOL,
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);
<?php
return [
'url' => 'https://amka-services.gunet.gr/api/rest/v1/ssn_validation',
'key' => 'place-your-auth-key-here',
'extra_headers' => [
// any custom headers as 'key' => 'value'
],
'verify_ssl' => false // only if ssl is not tuned correctly!
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment