forked from Sage/sageone_api_php_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsageone_response.php
More file actions
114 lines (93 loc) · 4.56 KB
/
sageone_response.php
File metadata and controls
114 lines (93 loc) · 4.56 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
<!DOCTYPE html>
<?php
include 'sageone_constants.php';
include 'sageone_signer.php';
include 'sageone_client.php';
$sageone_client = new SageoneClient($client_id, $client_secret, $callback_url, $auth_endpoint, $token_endpoint, $scope);
$nonce = bin2hex(openssl_random_pseudo_bytes(32));
$header = array("Accept: *.*",
"Content_Type: application/x-www-form-urlencoded",
"User-Agent: Sage One Sample Application");
if($_GET) {
switch(array_keys($_GET)[0]) {
case "get_endpoint":
$token = $_GET['get_access_token'];
$endpoint = $_GET['get_endpoint'];
$url = $base_endpoint . $endpoint;
/* body params are empty for a GET request */
$params = array();
/* generate the request signature */
$signature_object = new SageoneSigner("get", $url, $params, $nonce, $signing_secret, $token);
$signature = $signature_object->signature();
/* add the token, signature and nonce to the request header */
array_push($header, "Authorization: Bearer " . $token, "X-Signature: " . $signature, "X-Nonce: " . $nonce);
$response = $sageone_client->getData($url, $header);
break;
case "delete_endpoint":
$token = $_GET['delete_access_token'];
$endpoint = $_GET['delete_endpoint'];
$url = $base_endpoint . $endpoint;
/* body params are empty for a DELETE request */
$params = array();
/* generate the request signature */
$signature_object = new SageoneSigner("delete", $url, $params, $nonce, $signing_secret, $token);
$signature = $signature_object->signature();
/* add the token, signature and nonce to the request header */
array_push($header, "Authorization: Bearer " . $token, "X-Signature: " . $signature, "X-Nonce: " . $nonce);
$response = $sageone_client->deleteData($url, $header);
break;
case "put_endpoint":
$token = $_GET['put_access_token'];
$endpoint = $_GET['put_endpoint'];
$url = $base_endpoint . $endpoint;
$put_data = utf8_encode($_GET['put_data']);
/* get the body params as an array of key => value pairs */
$params = json_decode($put_data, true);
/* generate the request signature */
$signature_object = new SageoneSigner("put", $url, $params, $nonce, $signing_secret, $token);
$signature = $signature_object->signature();
/* add the token, signature and nonce to the request header */
array_push($header, "Authorization: Bearer " . $token, "X-Signature: " . $signature, "X-Nonce: " . $nonce);
$response = $sageone_client->putData($url, $params, $header);
break;
}
} else {
$token = $_POST['post_access_token'];
$endpoint = $_POST['post_endpoint'];
$url = $base_endpoint . $endpoint;
$post_data = utf8_encode($_POST['post_data']);
/* get the body params as an array of key => value pairs */
$params = json_decode($post_data, true);
/* generate the request signature */
$signature_object = new SageoneSigner("post", $url, $params, $nonce, $signing_secret, $token);
$signature = $signature_object->signature();
/* add the token, signature and nonce to the request header */
array_push($header, "Authorization: Bearer " . $token, "X-Signature: " . $signature, "X-Nonce: " . $nonce);
$response = $sageone_client->postData($url, $params, $header);
}
/* prettify JSON response for readability */
$json = json_decode($response);
$pretty_json = json_encode($json, JSON_PRETTY_PRINT);
?>
<html>
<head>
<title>Sage One Response</title>
<link type="text/css" rel="stylesheet" href="sample_app.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<a id="logo" href="/">Sage One API Sample App</a>
</div>
</header>
<div class="container">
<pre><?php echo $pretty_json; ?></pre>
</div>
</body>
</html>