-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHttpClient.php
184 lines (157 loc) · 3.91 KB
/
HttpClient.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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class HttpClient
* Simple HTTP client using cURL to make http requests easier
* Supports: POST, GET
*
* @author Elson Tan (@elsodev)
* @version 1.0
*/
class HttpClient
{
//properties
private $headers,
$data,
$url,
$ch,
$results,
$curl_returntransfer = true,
$curl_header = false,
$error = false,
$errorMsg;
/**
* Constructor
* @param $params
*/
public function __construct($params = NULL)
{
if(!is_null($params)){
$this->setOptions($params);
}
}
/**
* Set options
* @param $params
*/
public function setOptions($params){
foreach ($params as $key => $val)
{
$this->{$key} = $val;
}
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, $this->curl_returntransfer);
curl_setopt($this->ch, CURLOPT_HEADER, $this->curl_header);
}
/**
* Execute cURL
* @return bool
*/
private function execute(){
$this->error = false;
//executes the curl
$this->results = curl_exec($this->ch);
if(curl_errno($this->ch)){
//set error message
$this->error = true;
$this->errorMsg = curl_error($this->ch);
curl_close($this->ch);
return false;
} else {
curl_close($this->ch);
return true;
}
}
/**
* Get Results
* @return string
*/
public function getResults(){
return $this->results;
}
/**
* Get Results in array
* @return array
*/
public function getResultsArray(){
return json_decode( $this->results, true );
}
/**
* Get Error Status
* @return bool
*/
public function getError(){
return $this->error;
}
/**
* Get error message
* @return string
*/
public function getErrorMsg(){
return $this->errorMsg;
}
/**
* Check if supplied data is array, then convery it to query format.
* @param $d
* @return string
*/
private function queryData($d){
if (is_array($d)) {
$data = http_build_query($d);
} else {
$data = $d;
}
return $data;
}
/**
* Make sure URL and Data is provided
* @return bool
*/
private function checkEssentials(){
$this->error = false;
if (!empty($this->url) && !empty($this->data)) {
return true;
}
$this->error = true;
$this->errorMsg = 'No URL/Data provided';
return false;
}
/**
* Setup POST
* @return bool
*/
public function post(){
if ($this->checkEssentials()) {
$post_data = $this->queryData($this->data);
$options = array(
CURLOPT_URL => $this->url,
CURLOPT_POST => count($post_data),
CURLOPT_POSTFIELDS => $post_data,
);
curl_setopt_array ( $this->ch, $options );
//set headers
if( !empty($this->headers) ){
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
}
return $this->execute();
}
return false;
}
/**
* Setup GET
* @return bool
*/
public function get()
{
if ($this->checkEssentials()) {
$get_data = $this->queryData($this->data);
curl_setopt($this->ch, CURLOPT_URL, $this->url . '?' . $get_data);
//set headers
if (!empty($this->headers)) {
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
}
return $this->execute();
}
return false;
}
}
/* End of file HttpClient.php */