-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_connector.php
77 lines (61 loc) · 1.63 KB
/
api_connector.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
---
### فایل `src/api_connector.php`
```php
<?php
// Set your API details here
define('API_URL', 'https://api.example.com/v1/resource');
define('API_KEY', 'your_api_key');
/**
* Send a GET request to the API.
*/
function getData() {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => API_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . API_KEY
]
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "Error during GET request: $error\n";
} else {
echo "GET Response:\n$response\n";
}
}
/**
* Send a POST request to the API.
*
* @param array $data The data to send in the POST request.
*/
function postData(array $data) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => API_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . API_KEY,
'Content-Type: application/json'
]
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "Error during POST request: $error\n";
} else {
echo "POST Response:\n$response\n";
}
}
// Example usage
echo "Performing GET request...\n";
getData();
echo "\nPerforming POST request...\n";
$data = ['key' => 'value']; // Replace with your data
postData($data);
?>