forked from Sage/sageone_api_php_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsageone_client.php
More file actions
130 lines (108 loc) · 4.39 KB
/
sageone_client.php
File metadata and controls
130 lines (108 loc) · 4.39 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
class SageoneClient {
private $client_id;
private $client_secret;
private $callback_url;
private $auth_endpoint;
private $token_endpoint;
private $scope;
/**
* @param string $client_id Your application's client_id
* @param string $client_secret Your application's client_secret
* @param string $callback_url Your application's callback_url
* @param string $auth_endpoint The authorisation endpoint (https://www.sageone.com/oauth2/auth)
* @param string $token_endpoint The token endpoint (https://api.sageone.com/oauth2/token)
* @param string $scope The type of access - readonly or full_access
*/
public function __construct($client_id, $client_secret, $callback_url, $auth_endpoint, $token_endpoint, $scope) {
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->callback_url = $callback_url;
$this->auth_endpoint = $auth_endpoint;
$this->token_endpoint = $token_endpoint;
$this->scope = $scope;
}
/**
* Returns the redirect url with required query params for hitting the
* authorisation endpoint
*/
public function authRedirect() {
return $this->auth_endpoint . "?response_type=code&client_id=" . $this->client_id . "&redirect_uri=" . $this->callback_url . "&scope=" . $this->scope;
}
/* POST request to exchange the authorisation code for an access_token */
public function getAccessToken($code) {
$params = array("client_id" => $this->client_id,
"client_secret" => $this->client_secret,
"code" => $code,
"grant_type" => "authorization_code",
"redirect_uri" => $this->callback_url);
$response = $this->getToken($params);
return $response;
}
/* POST request to renew the access_token */
public function renewAccessToken($refresh_token) {
$params = array("client_id" => $this->client_id,
"client_secret" => $this->client_secret,
"refresh_token" => $refresh_token,
"grant_type" => "refresh_token");
$response = $this->getToken($params);
return $response;
}
/* GET request */
public function getData($endpoint, $header) {
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($curl);
if (!$response) { /* Handle error */ }
return $response;
}
/* POST request */
public function postData($endpoint, $post_data, $header) {
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($curl);
if (!$response) { /* Handle error */ }
return $response;
}
/* PUT request */
public function putData($endpoint, $put_data, $header) {
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($put_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($curl);
if (!$response) { /* Handle error */ }
return $response;
}
/* DELETE request */
public function deleteData($endpoint, $header) {
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($curl);
if (!$response) { /* Handle error */ }
return $response;
}
private function getToken($params) {
$url = $this->token_endpoint;
$options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($params)));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
return $result;
}
}
?>