-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthorisationCode.php
More file actions
130 lines (103 loc) · 5.34 KB
/
AuthorisationCode.php
File metadata and controls
130 lines (103 loc) · 5.34 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
require('/vendor/autoload.php');
$buildURL = 'https://client.simprosuite.com'; //Actual url for your customer's build
$clientID = 'xxx'; //From the key file you received
$clientSecret = 'xxx'; //From the key file you received
//Create an access token for this user. You only need to call this once per user.
createAccessToken($buildURL, $clientID, $clientSecret);
//On subsequent loads, you don't need the user to login again. Just use the saved access token.
//You will still need the build url, client id, and client secret.
$provider = loadAccessToken($buildURL, $clientID, $clientSecret);
//The system will automatically handle refreshing your token. But if, for whatever reason, you need to refresh it manually:
//$provider->refreshAccessToken();
//Now, fetch and select a company ID. For most builds, there is only one company id.
$companyID = getCompanyID($provider);
//Then, list all employees and select one to view full details.
$employeeID = selectEmployeeFromList($provider, $companyID);
//Now let's get the full information for this employee.
viewEmployeeDetails($provider, $companyID, $employeeID);
function createAccessToken($buildURL, $clientID, $clientSecret)
{
//Load the authorisation code provider using your details.
$provider = (new \simPRO\RestClient\OAuth2\AuthorisationCode())
->withBuildURL($buildURL)
->withClientDetails($clientID, $clientSecret)
;
//Now, the user needs to log into simPRO and authorise your application.
//The first step is to redirect them to simPRO.
//After a time, simPRO will redirect them back to your application with either an authorisation code or an error.
//If there's an error, you will need to handle it. If it's successful, we can proceed.
//=========================================================================================================
// If it doesn't redirect properly, then your application's Redirect URI may not be correctly configured.
// See http://developer.simprogroup.com/apidoc/?page=5b265889ce433f9710dcef721505c158
//=========================================================================================================
if (isset($_GET['error'])) {
//The authorisation server returned an error.
throw new \Exception($_GET['error_description']);
} elseif (isset($_GET['code'])) {
//The authorisation server successfully returned a code. Use it.
$provider->withCode($_GET['code']);
} else {
//We don't have an authorisation code yet.
//Redirect the user to simPRO so they can log in and authorise your application.
header('Location: ' . $provider->getBaseAuthorizationUrl());
exit();
}
//Now that we are fully authenticated, fetch the access token.
$accessTokenData = $provider->getAccessTokenObject()->jsonSerialize();
print "Access token:" . PHP_EOL;
print json_encode($accessTokenData, JSON_PRETTY_PRINT) . PHP_EOL . PHP_EOL;
//And store it in your database.
$yourDatabase->saveAccessToken($accessTokenData);
}
function loadAccessToken($buildURL, $clientID, $clientSecret)
{
//Load it from your database.
$accessTokenData = $yourDatabase->loadAccessToken();
//Use the access token provider instead.
$provider = (new \simPRO\RestClient\OAuth2\AccessToken())
->withBuildURL($buildURL)
->withClientDetails($clientID, $clientSecret)
->withAccessTokenObject($accessTokenData)
;
return $provider;
}
function getCompanyID($provider)
{
$companyURL = '/api/v1.0/companies/';
print "Now calling URL {$companyURL}" . PHP_EOL;
$request = $provider->fetchRequest($companyURL); //PSR7 Request Object
$response = $provider->fetchResponse($request); //PSR7 Response Object
$companyArray = json_decode((string)$response->getBody());
print "Response is:" . PHP_EOL;
print json_encode($companyArray, JSON_PRETTY_PRINT) . PHP_EOL . PHP_EOL;
foreach ($companyArray as $companyObject) {
print "Company with ID " . $companyObject->ID . " is named " . $companyObject->Name . PHP_EOL;
}
//Select which company ID you wish to use.
$companyID = $companyArray[count($companyArray)-1]->ID;
print "Using company id {$companyID}" . PHP_EOL . PHP_EOL;
return $companyID;
}
function selectEmployeeFromList($provider, $companyID)
{
//Fetch a list of employees
$employeeListURL = "/api/v1.0/companies/{$companyID}/employees/";
print "Now calling URL {$employeeListURL}" . PHP_EOL;
$request = $provider->fetchRequest($employeeListURL);
$employeeArray = $provider->fetchJSON($request); //Can use fetchJSON instead of fetchResponse to just get the JSON.
print 'Employee Count: ' . count($employeeArray) . PHP_EOL;
//Select an employee.
$employeeObject = $employeeArray[mt_rand(0, count($employeeArray)-1)];
print 'Selected employee: ' . PHP_EOL;
print json_encode($employeeObject, JSON_PRETTY_PRINT) . PHP_EOL . PHP_EOL;
$employeeID = $employeeObject->ID;
return $employeeID;
}
function viewEmployeeDetails($provider, $companyID, $employeeID)
{
$employeeGetURL = "/api/v1.0/companies/{$companyID}/employees/{$employeeID}";
print "Now calling URL {$employeeGetURL}" . PHP_EOL;
$employeeInfo = $provider->fetchJSON($provider->fetchRequest($employeeGetURL)); //Can combine into one line.
print json_encode($employeeInfo, JSON_PRETTY_PRINT) . PHP_EOL;
}