master
Bernd Wurst API-Authtoken, Idle-Warnung...

Bernd Wurst authored 3 years ago

1) <?php
2) 
Bernd Wurst API-Aufrufe über Guzzle um...

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 3 years ago

14) 
Bernd Wurst reuse JWT token

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 3 years ago

78) 
Bernd Wurst reuse JWT token

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 3 years ago

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

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 7 months ago

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

Bernd Wurst authored 8 months ago

103)     if ($res->getStatusCode() >= 500) {
104)         print('Es gibt gerade ein technisches Problem.');
105)         die();
106)     }
Bernd Wurst Fange ungültige Telefonnumm...

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 8 months ago

111)     return [
112)         "status_code" => $res->getStatusCode(),
113)         "data" => json_decode( $res->getBody(), true )
114)         ];
115) }
116) 
117) 
Bernd Wurst Neue API 2023

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 3 years ago

119) 
Bernd Wurst Neue API 2023

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 3 years ago

126)     }
Bernd Wurst Neue API 2023

Bernd Wurst authored 8 months ago

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

Bernd Wurst authored 3 years ago

129)     }
Bernd Wurst Neue API 2023

Bernd Wurst authored 8 months ago

130)     return NULL;