Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. <?php
  2. $baseUrl = 'https://shaarli.mydomain.net';
  3. $secret = 'thats_my_api_secret';
  4.  
  5. function base64url_encode($data) {
  6.   return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  7. }
  8.  
  9. function generateToken($secret) {
  10.     $header = base64url_encode('{
  11.        "typ": "JWT",
  12.        "alg": "HS512"
  13.    }');
  14.     $payload = base64url_encode('{
  15.        "iat": '. time() .'
  16.    }');
  17.     $signature = base64url_encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
  18.     return $header . '.' . $payload . '.' . $signature;
  19. }
  20.  
  21.  
  22. function getInfo($baseUrl, $secret) {
  23.     $token = generateToken($secret);
  24.     $endpoint = rtrim($baseUrl, '/') . '/api/v1/info';
  25.  
  26.     $headers = [
  27.         'Content-Type: text/plain; charset=UTF-8',
  28.         'Authorization: Bearer ' . $token,
  29.     ];
  30.  
  31.     $ch = curl_init($endpoint);
  32.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  33.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34.     curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  35.     curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  36.  
  37.     $result = curl_exec($ch);
  38.     curl_close($ch);
  39.  
  40.     return $result;
  41. }
  42.  
  43. var_dump(getInfo($baseUrl, $secret));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement