-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.php
More file actions
76 lines (71 loc) · 2.38 KB
/
request.php
File metadata and controls
76 lines (71 loc) · 2.38 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
<?php
/**
* @author Fachpersonal
* @version 1.0
*/
class Request
{
/**
* Allows to make a GET Request, by parsing the url and data (if needed).
*
* @param string $url API URL
* @param array $data Additional Data which are safed as POSTFIELDS, not always needed
* @return array response as JSON Object
*/
public static function get(string $url, array $data = []) : array
{
return self::call($url, "GET", $data);
}
/**
* Allows to make a POST Request, by parsing the url and data (if needed).
*
* @param string $url API URL
* @param array $data Additional Data which are safed as POSTFIELDS, not always needed
* @return array response as JSON Object
*/
public static function post(string $url, array $data = []) : array
{
return self::call($url, "POST", $data);
}
/**
* Allows to make a PUT Request, by parsing the url and data (if needed).
*
* @param string $url API URL
* @param array $data Additional Data which are safed as POSTFIELDS, not always needed
* @return array response as JSON Object
*/
public static function put(string $url, array $data = []) : array
{
return self::call($url, "PUT", $data);
}
/**
* Allows to make a DELETE Request, by parsing the url and data (if needed).
*
* @param string $url API URL
* @param array $data Additional Data which are safed as POSTFIELDS, not always needed
* @return array response as JSON Object
*/
public static function delete(string $url, array $data = []) : array
{
return self::call($url, "DELETE", $data);
}
/**
* @param string $url API url
* @param string $method Request method (GET/POST/PUT/DELETE)
* @param array $data Additional Data if needed
* @return array response as JSON Object
*/
private static function call(string $url, string $method, array $data) : array{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($data != [])
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
));
$response = curl_exec($ch);
return json_decode($response);
}
}