git.schokokeks.org
Repositories
Help
Report an Issue
bibweb.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
cbb3ed9
Branches
Tags
master
bibweb.git
lib
api.php
frage E-Mail-Adresse ab
Bernd Wurst
commited
cbb3ed9
at 2023-09-22 09:56:08
api.php
Blame
History
Raw
<?php use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; require_once("config.php"); $base_url = $config['api_url']; $TOKEN_FILE = '../jwt-token.json'; $jwt = [ "token" => null, "refresh_token" => null, "exp" => time() ]; if (file_exists($TOKEN_FILE)) { $jwt = json_decode(file_get_contents($TOKEN_FILE), true); } if ($jwt['refresh_token'] && $jwt['exp'] < time()+5) { // refresh token $url = $base_url . 'token/refresh'; $data = ["refresh_token" => $jwt['refresh_token']]; $options = array( 'http' => array( 'method' => 'POST', 'content' => json_encode( $data ), 'header'=> "Content-Type: application/json\r\n" . "Accept: application/json\r\n" ) ); $context = stream_context_create( $options ); $result = @file_get_contents( $url, false, $context ); if (!$result) { print('Es gibt gerade ein technisches Problem.'); die(); } $response = json_decode( $result, true); $jwt['token'] = $response["token"]; $jwt['refresh_token'] = $response["refresh_token"]; $fields = explode('.', $jwt['token']); $data = json_decode(base64_decode($fields[1]), true); $jwt['exp'] = $data['exp']; } if (!$jwt['token'] || $jwt['exp'] < time()+5) { // get new token # init API $url = $base_url . 'login'; $data = [ "username" => $config['api_username'], "password" => $config['api_password'] ]; $options = array( 'http' => array( 'method' => 'POST', 'content' => json_encode( $data ), 'header'=> "Content-Type: application/json\r\n" . "Accept: application/json\r\n" ) ); $context = stream_context_create( $options ); $result = @file_get_contents( $url, false, $context ); if (!$result) { print('Es gibt gerade ein technisches Problem.'); die(); } $response = json_decode( $result, true); $jwt['token'] = $response["token"]; $jwt['refresh_token'] = $response["refresh_token"]; $fields = explode('.', $jwt['token']); $data = json_decode(base64_decode($fields[1]), true); $jwt['exp'] = $data['exp']; } file_put_contents($TOKEN_FILE, json_encode($jwt)); function api_call($method, $url, $content = []) { global $jwt; global $base_url; $url = $base_url . $url; $headers = [ "Content-Type" => "application/json", "Accept" => "application/json", "Authorization" => "Bearer ".$jwt['token'] ]; $body = json_encode( $content ); $client = new Client(["http_errors" => false]); $request = new Request(strtoupper($method), $url, $headers, $body); $res = $client->send($request, ['timeout' => 5]); //$filename = '../data/'.date('Y').'/debug-'.microtime(true).'.json'; //@file_put_contents($filename, strtoupper($method).' '.$url."\n".$body."\n".$res->getBody()); if ($res->getStatusCode() >= 500) { print('Es gibt gerade ein technisches Problem.'); die(); } if ($res->getStatusCode() >= 400) { //print($res->getBody()); //die(); } return [ "status_code" => $res->getStatusCode(), "data" => json_decode( $res->getBody(), true ) ]; } require_once('vendor/autoload.php'); function format_number_api($number) { $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $phoneNumber = $phoneNumberUtil->parse($number, 'DE'); } catch (Exception $e) { return NULL; } if ($phoneNumberUtil->isValidNumber($phoneNumber)) { return $phoneNumberUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::E164); } return NULL; }