forked from GitMahalo/mahalo-php-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resttbs.php
223 lines (185 loc) · 6.29 KB
/
resttbs.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
if(!@include("param.php")) die("Pour utiliser les examples, renommer param.exemple.php et renseignez les valeurs requises");
header( 'content-type: text/html; charset= iso-8859-1' );
$urls = [
"PROD" => [
"PORTAL" => "https://portal.mahalo-app.io/oauth/token",
"WS" => "https://api.mahalo-app.io/aboweb"
],
"PREPROD" => [
"PORTAL" => "https://portal-preprod.mahalo-app.io/oauth/token",
"WS" => "https://api-preprod.mahalo-app.io/aboweb"
],
"LOCAL" => [
"PORTAL" => "https://localhost:8443/aboweb-portal/oauth/token",
"WS" => "https://localhost:8443/aboweb-ws"
]
];
if($urls[TARGET] === null){
die("TARGET ".TARGET." not found");
}
function callApiGet($url, $token, $datas = null) {
global $urls;
$headers = array(
'Authorization: BEARER '.$token,
'Content-type: text/html; charset=utf-8'
);
$url_with_datas = $url;
if($datas !== null){
$url_with_datas .= '?'.http_build_query($datas);
}
return callApi($urls[TARGET]["WS"].$url_with_datas, "", "GET", $headers);
}
function callApiGetTypePdf($url, $token, $datas = null) {
global $urls;
$headers = array(
'Authorization: BEARER '.$token,
'Accept: application/pdf'
);
$url_with_datas = $url;
if($datas !== null){
$url_with_datas .= '?'.http_build_query($datas);
}
return callApi($urls[TARGET]["WS"].$url_with_datas, "", "GET", $headers, false);
}
function callApiPut($url, $token, $datas) {
global $urls;
$data_string = json_encode($datas);
$headers = array(
'Authorization: BEARER '.$token,
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
);
return callApi($urls[TARGET]["WS"].$url, $data_string, "PUT", $headers);
}
function callApiPost($url, $token, $datas, $datasQuery = null) {
global $urls;
$data_string = json_encode($datas);
$headers = array(
'Authorization: BEARER '.$token,
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
);
$url_with_datas = $url;
if($datasQuery !== null){
$url_with_datas .= '?'.http_build_query($datasQuery);
}
return callApi($urls[TARGET]["WS"].$url_with_datas, $data_string, "POST", $headers);
}
function callApiPatch($url, $token, $datas) {
global $urls;
$data_string = json_encode($datas);
$headers = array(
'Authorization: BEARER '.$token,
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
);
return callApi($urls[TARGET]["WS"].$url, $data_string, "PATCH", $headers);
}
function callApi($url, $data_string, $verb="GET", $headers, $json=true, $token=false) {
$curl = curl_init();
$opts = [
CURLOPT_URL => $url,
CURLOPT_POST => false,
CURLOPT_CUSTOMREQUEST => $verb,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
];
if($verb !== "GET") {
$opts[CURLOPT_POST] = true;
$opts[CURLOPT_POSTFIELDS] = $data_string;
}
if($json === false && $token === false){// cas ou on veut afficher un pdf (mais pas pour l'appel WS Token)
$opts[CURLOPT_HEADER] = true;
}
curl_setopt_array($curl, $opts);
$executionStartTime = microtime(true);
$response = curl_exec($curl);
$executionEndTime = microtime(true);
$seconds = $executionEndTime - $executionStartTime;
if($json === true){ // on n'affiche pas de message pour le pdf (cas ou on appelle le WS Token)
print "REPONSE en $seconds secondes<br>";
print_r($response);
print "<br>FIN REPONSE<br><br>";
}
if($json === true || $token === true){
$response = json_decode($response);
} else {
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
transferHeader($headers);
$body = substr($response, $header_size);
echo $body;
/*
## Alternative 1 : contenu transformé en data-url encodé en base64, accessible depuis un lien.
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
echo '<a href="data:'.$contentType.';base64,'.base64_encode($response).'" download="download.pdf">Download</a>';
## Alternative 2 : enregistrement du contenu dans un répertoire du serveur
file_put_contents('download.pdf', $response);
*/
}
curl_close($curl);
return $response;
}
function transferHeader($headers) {
$dataHeader = explode("\r\n", $headers);
foreach($dataHeader as $val) {
header($val);
}
}
function getToken($username, $password, $json=true) {
global $urls;
if($json === true){ // on affiche que dans le cas ou on veut du json
print "Recuperation du token<br>";
}
$params = [
'grant_type' => 'password',
'username' => $username,
'password' => $password,
];
$data_string = http_build_query($params);
$headers = array(
'Authorization: Basic YWJvd2ViOg==',
'Content-Length: ' . strlen($data_string),
'Content-Type: application/x-www-form-urlencoded'
);
$response = callApi($urls[TARGET]["PORTAL"], $data_string, "POST", $headers, $json, true);
if($json === true){
print "TOKEN API : ".$response->access_token."<br><br>";
}
return $response->access_token;
/*$curl = curl_init();
$params = [
'grant_type' => 'password',
'username' => $username,
'password' => $password,
];
$data_string = http_build_query($params);
//print_r($data_string);
//print_r($urls[TARGET]["PORTAL"]);
$opts = [
CURLOPT_URL => $urls[TARGET]["PORTAL"],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
'Authorization: Basic YWJvd2ViOg==',
'Content-Length: ' . strlen($data_string),
'Content-Type: application/x-www-form-urlencoded'
),
];
curl_setopt_array($curl, $opts);
// curl_setopt($curl, CURLOPT_VERBOSE, 1);
// curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
curl_close($curl);
// print_r($response);
$response = json_decode($response);
print "TOKEN API : ".$response->access_token."<br><br>";
return $response->access_token;*/
}
?>