/
parasut-api-call.php
51 lines (46 loc) · 1.92 KB
/
parasut-api-call.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
require('oauth2/Client.php');
require("oauth2/GrantType/IGrantType.php");
require("oauth2/GrantType/AuthorizationCode.php");
require("oauth2/GrantType/RefreshToken.php");
const CLIENT_ID = 'CLIENT_ID';
const CLIENT_SECRET = 'CLIENT_SECRET';
const REDIRECT_URI = 'REDIRECT_URI';
const AUTHORIZATION_ENDPOINT = 'https://api.parasut.com/oauth/authorize';
const TOKEN_ENDPOINT = 'https://api.parasut.com/oauth/token';
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code']))
{
// authentication call
$auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
header('Location: ' . $auth_url);
die('Redirect');
}
else
{
// access token request
$params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
// CAUTION! instead of parse_str, assign response directly to $info or find an alternative way.
// parse_str($response['result'], $info);
$info = $response['result'];
$accessToken = $info['access_token'];
$refreshToken = $info['refresh_token'];
print_r($info);
echo "<br /><br /><br />";
// set access token for further api calls
$client->setAccessToken($accessToken);
// example api call
$response = $client->fetch('https://api.parasut.com/v1/me');
var_dump($response, $response['result']);
echo "<br /><br /><br />";
// in the case of token expiration, use method below
$params = array('refresh_token' => $refreshToken);
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'refresh_token', $params);
$info = $response['result'];
print_r($info);
$client->setAccessToken($info['access_token']);
$response = $client->fetch('https://api.parasut.com/v1/33/sales_invoices');
var_dump($response, $response['result']);
}
die;