git.schokokeks.org
Repositories
Help
Report an Issue
bibweb.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
9a2390f
Branches
Tags
master
bibweb.git
lib
api.php
reuse JWT token
Bernd Wurst
commited
9a2390f
at 2023-08-05 11:33:04
api.php
Blame
History
Raw
<?php 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['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 ); $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; $options = array( 'http' => array( 'method' => strtoupper($method), 'content' => json_encode( $content ), 'header'=> "Content-Type: application/json\r\n" . "Accept: application/json\r\n" . "Authorization: Bearer ".$jwt['token']."\r\n" ) ); $context = stream_context_create( $options ); $result = file_get_contents( $url, false, $context ); $response = json_decode( $result, true ); return $response; } 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; }