This repository was archived by the owner on Jul 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
110 lines (87 loc) · 2.44 KB
/
api.php
File metadata and controls
110 lines (87 loc) · 2.44 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
<?php
function getApiUrl()
{
global $wpdb;
$table_name = $wpdb->prefix . 'intersect_connect';
$data = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
return $data[3]['value'];
}
function getToken()
{
global $wpdb;
$table_name = $wpdb->prefix . 'intersect_connect';
$data = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
return $data[2]['value'];
}
function get()
{
}
function post()
{
}
function getAuth($endpoint)
{
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . getToken(),
);
$args = array(
'headers' => $headers,
'sslverify' => false,
);
$response = wp_remote_get(getApiUrl() . $endpoint, $args);
if (is_wp_error($response)) {
die('Error: ' . $response->get_error_message());
// return ''; // Gérer l'erreur selon vos besoins
}
$response_body = wp_remote_retrieve_body($response);
return json_decode($response_body, true);
}
function postAuth(array $postData, string $endpoint)
{
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . getToken(),
);
$args = array(
'body' => json_encode($postData),
'headers' => $headers,
'sslverify' => false,
);
$response = wp_remote_post(getApiUrl() . $endpoint, $args);
$response_body = wp_remote_retrieve_body($response);
return json_decode($response_body, true);
}
function auth(array $authData, string $api_url): string
{
$headers = array(
'Content-Type' => 'application/json',
);
$args = array(
'body' => json_encode($authData),
'headers' => $headers,
'sslverify' => false,
);
$response = wp_remote_post($api_url . '/api/oauth/token', $args);
if (is_wp_error($response)) {
die('Error: ' . $response->get_error_message());
// return ''; // Gérer l'erreur selon vos besoins
}
$response_body = wp_remote_retrieve_body($response);
$response_data = json_decode($response_body, true);
return $response_data["access_token"];
}
function getUser(string $username): array
{
$user = getAuth("/api/v1/users/" . $username);
return $user;
}
function register(array $postData): bool
{
$register = postAuth($postData, "/api/v1/users/register");
die(var_dump($register));
if (!isset($register["username"])) {
return false;
}
return true;
}