-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurl.php
89 lines (75 loc) · 2.41 KB
/
curl.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
<?php
class Curl {
private $showError;
private $json;
private $data;
private $request;
public function __construct(){
$this->showError = false;
$this->json = true;
$this->data = array();
$this->request = "GET";
}
/**
* If error should be shown upon request failure
* @param bool|boolean $display True/False
* @return Sets the $showError variable
*/
public function showError(bool $display = false){
$this->showError = $display;
}
/**
* If the data should be treated as JSON
* @param bool|boolean $json True/False
* @return Sets the $json variable
*/
public function jsonDecode(bool $json = true){
$this->json = $json;
}
/**
* If you're doing a POST requests
* @param array $data POST values
* @return Sets the $data variable and holds the data for execution
*/
public function post(array $data){
$data = json_encode($data);
$this->data = $data;
$this->request = "POST";
}
/**
* If you're doing PUT requests
* @param array $data PUT values
* @return Sets the $data variable and holds the data for execution
*/
public function put(array $data){
$data = json_encode($data);
$this->data = $data;
$this->request = "PUT";
}
/**
* Performs the query
* @param string $url The URL to request/post to
* @return string|array The responds
*/
public function curl(string $url) {
$init = curl_init($url);
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($init, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($init, CURLOPT_TIMEOUT, 10);
if($this->showError == true)
curl_setopt($init, CURLOPT_FAILONERROR, true);
if($this->data != null && !empty($this->data)){
curl_setopt($init, CURLOPT_CUSTOMREQUEST, $this->request);
curl_setopt($init, CURLOPT_POSTFIELDS, $this->data);
curl_setopt($init, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($this->data)));
}
$data = curl_exec($init);
curl_close($init);
if($this->json == true)
$data = json_decode($data, true);
return $data;
}
}
?>