6fbcee619e1fddcd223537b48395850cf504ac61
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

1) <?php
Bernd Wurst autoload mit festem Pfad

Bernd Wurst authored 1 month ago

2) require_once(__DIR__.'/../vendor/autoload.php');
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

3) 
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

4) use GuzzleHttp\Client;
5) use GuzzleHttp\Psr7\Request;
Bernd Wurst Neue API 2023

Bernd Wurst authored 1 year ago

6) require_once("config.php");
7) $base_url = $config['api_url'];
Bernd Wurst reuse JWT token

Bernd Wurst authored 1 year ago

8) 
9) $TOKEN_FILE = '../jwt-token.json';
10) $jwt = [
11)     "token" => null,
12)     "refresh_token" => null,
13)     "exp" => time()
Bernd Wurst Neue API 2023

Bernd Wurst authored 1 year ago

14) ];
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

15) 
Bernd Wurst reuse JWT token

Bernd Wurst authored 1 year ago

16) if (file_exists($TOKEN_FILE)) {
17)     $jwt = json_decode(file_get_contents($TOKEN_FILE), true);
18) }
19) 
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

20) if ($jwt['refresh_token'] && $jwt['exp'] < time()+5) {
21)     // refresh token
22)     $url = $base_url . 'token/refresh';
23)     $data = ["refresh_token" => $jwt['refresh_token']];
24)     $options = array(
25)       'http' => array(
26)         'method'  => 'POST',
27)         'content' => json_encode( $data ),
28)         'header'=>  "Content-Type: application/json\r\n" .
29)                     "Accept: application/json\r\n"
30)         )
31)     );
32) 
33)     $context = stream_context_create( $options );
34)     $result = @file_get_contents( $url, false, $context );
35)     if (!$result) {
36)         print('Es gibt gerade ein technisches Problem.');
37)         die();
38)     }
39)     $response = json_decode( $result, true);
40) 
41)     $jwt['token'] = $response["token"];
42)     $jwt['refresh_token'] = $response["refresh_token"];
43)     $fields = explode('.', $jwt['token']);
44)     $data = json_decode(base64_decode($fields[1]), true);
45)     $jwt['exp'] = $data['exp'];
46) }
47) 
Bernd Wurst reuse JWT token

Bernd Wurst authored 1 year ago

48) if (!$jwt['token'] || $jwt['exp'] < time()+5) {
49)     // get new token
50)     # init API
51)     $url = $base_url . 'login';
52)     $data = [
53)         "username" => $config['api_username'],
54)         "password" => $config['api_password']
55)     ];
56)     $options = array(
57)       'http' => array(
58)         'method'  => 'POST',
59)         'content' => json_encode( $data ),
60)         'header'=>  "Content-Type: application/json\r\n" .
61)                     "Accept: application/json\r\n"
62)         )
63)     );
64) 
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

65)     $context = stream_context_create( $options );
66)     $result = @file_get_contents( $url, false, $context );
67)     if (!$result) {
68)         print('Es gibt gerade ein technisches Problem.');
69)         die();
70)     }
Bernd Wurst reuse JWT token

Bernd Wurst authored 1 year ago

71)     $response = json_decode( $result, true);
72) 
73)     $jwt['token'] = $response["token"];
74)     $jwt['refresh_token'] = $response["refresh_token"];
75)     $fields = explode('.', $jwt['token']);
76)     $data = json_decode(base64_decode($fields[1]), true);
77)     $jwt['exp'] = $data['exp'];
78) }
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

79) 
Bernd Wurst reuse JWT token

Bernd Wurst authored 1 year ago

80) file_put_contents($TOKEN_FILE, json_encode($jwt));
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

81) 
82) 
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

83) function api_call($method, $url, $content = []) 
84) {
85)     global $jwt;
86)     global $base_url;
87)     $url = $base_url . $url;
88) 
89) 
90)     $headers = [
91)         "Content-Type" => "application/json",
92)         "Accept" => "application/json",
93)         "Authorization" => "Bearer ".$jwt['token']
94)         ];
95)     $body = json_encode( $content );
96)     $client = new Client(["http_errors" => false]);
97)     $request = new Request(strtoupper($method), $url, $headers, $body);
98)     $res = $client->send($request, ['timeout' => 5]);
99) 
Bernd Wurst frage E-Mail-Adresse ab

Bernd Wurst authored 1 year ago

100)     //$filename = '../data/'.date('Y').'/debug-'.microtime(true).'.json';
101)     //@file_put_contents($filename, strtoupper($method).' '.$url."\n".$body."\n".$res->getBody());
102) 
103) 
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

104)     if ($res->getStatusCode() >= 500) {
105)         print('Es gibt gerade ein technisches Problem.');
Bernd Wurst Session zerstören wenn der...

Bernd Wurst authored 2 months ago

106)         //print_r($request);
107)         //print_r($res->getBody());
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

108)         die();
109)     }
Bernd Wurst Fange ungültige Telefonnumm...

Bernd Wurst authored 1 year ago

110)     if ($res->getStatusCode() >= 400) {
111)         //print($res->getBody()); 
112)         //die();
113)     }
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 1 year ago

114)     return [
115)         "status_code" => $res->getStatusCode(),
116)         "data" => json_decode( $res->getBody(), true )
117)         ];
118) }
119) 
120) 
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

121) 
Bernd Wurst Neue API 2023

Bernd Wurst authored 1 year ago

122) function format_number_api($number) {
123)     $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
124)     try {
125)         $phoneNumber = $phoneNumberUtil->parse($number, 'DE');
126)     } catch (Exception $e) {
127)         return NULL;
Bernd Wurst besserer Debug-Modus, Handh...

Bernd Wurst authored 4 years ago

128)     }
Bernd Wurst Neue API 2023

Bernd Wurst authored 1 year ago

129)     if ($phoneNumberUtil->isValidNumber($phoneNumber)) {
130)         return $phoneNumberUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::E164);
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 4 years ago

131)     }
Bernd Wurst Neue API 2023

Bernd Wurst authored 1 year ago

132)     return NULL;