|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
|
5
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
|
6
|
|
|
* |
|
7
|
|
|
* Licensed under The MIT License |
|
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
10
|
|
|
* |
|
11
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
|
12
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
|
13
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
|
14
|
|
|
* @author Marcio Zebedeu - [email protected] |
|
15
|
|
|
* @version 1.0.2 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Ballybran\Core\Http\Client; |
|
19
|
|
|
|
|
20
|
|
|
use \Ballybran\Core\Http\Encodes; |
|
21
|
|
|
use Ballybran\Helpers\Http\Http; |
|
22
|
|
|
|
|
23
|
|
|
class ClientRest extends Encodes |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
private $cookies; |
|
27
|
|
|
private $headers = []; |
|
28
|
|
|
private $user_agent; |
|
29
|
|
|
private $compression; |
|
30
|
|
|
private $cookie_file; |
|
31
|
|
|
private $proxy; |
|
32
|
|
|
|
|
33
|
|
|
function __construct($cookies = true, $cookie = "", $compression = 'gzip', $proxy = '') |
|
34
|
|
|
{ |
|
35
|
|
|
$this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; |
|
36
|
|
|
$this->headers[] = 'Connection: Keep-Alive'; |
|
37
|
|
|
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; |
|
38
|
|
|
$this->user_agent = Http::http_user_agent(); |
|
39
|
|
|
$this->compression = $compression; |
|
40
|
|
|
$this->proxy = $proxy; |
|
41
|
|
|
$this->cookies = $cookies; |
|
42
|
|
|
if ($this->cookies == true) { |
|
43
|
|
|
$this->cookie($cookie); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function cookie($cookie_file) |
|
49
|
|
|
{ |
|
50
|
|
|
if (file_exists($cookie_file)) { |
|
51
|
|
|
$this->cookie_file = $cookie_file; |
|
52
|
|
|
} else { |
|
53
|
|
|
fopen($cookie_file, 'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions'); |
|
54
|
|
|
$this->cookie_file = $cookie_file; |
|
55
|
|
|
fclose($cookie_file); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function get($url, $fields = null, $httpheader = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$process = curl_init($url); |
|
62
|
|
|
curl_setopt($process, CURLOPT_HTTPHEADER, $httpheader); |
|
63
|
|
|
curl_setopt($process, CURLOPT_HEADER, 0); |
|
64
|
|
|
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); |
|
65
|
|
|
if ($this->cookies == true) { |
|
66
|
|
|
curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file); |
|
67
|
|
|
} |
|
68
|
|
|
if ($this->cookies == true) { |
|
69
|
|
|
curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file); |
|
70
|
|
|
} |
|
71
|
|
|
curl_setopt($process, CURLOPT_ENCODING, $this->compression); |
|
72
|
|
|
curl_setopt($process, CURLOPT_TIMEOUT, 30); |
|
73
|
|
|
if ($this->proxy) { |
|
74
|
|
|
curl_setopt($process, CURLOPT_PROXY, $this->proxy); |
|
75
|
|
|
} |
|
76
|
|
|
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); |
|
77
|
|
|
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); |
|
78
|
|
|
$return = curl_exec($process); |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
// Close the cURL resource, and free system resources |
|
82
|
|
|
curl_close($process); |
|
83
|
|
|
$decoded = json_decode($return); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { |
|
86
|
|
|
die('error occured: ' . $decoded->response->errormessage); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
|
89
|
|
|
//if not joson, is xml' |
|
90
|
|
|
return ($decoded); |
|
91
|
|
|
|
|
92
|
|
|
} else { |
|
93
|
|
|
// if json true, return json |
|
94
|
|
|
return ($return); |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function post($url, $data, $httpheader = null ) |
|
101
|
|
|
{ |
|
102
|
|
|
$process = curl_init($url); |
|
103
|
|
|
if(null == $httpheader){ |
|
104
|
|
|
$httpheader = $this->headers; |
|
105
|
|
|
} |
|
106
|
|
|
curl_setopt($process, CURLOPT_HTTPHEADER, $httpheader); |
|
107
|
|
|
// curl_setopt($process, CURLOPT_HEADER, 1); |
|
108
|
|
|
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); |
|
109
|
|
|
if ($this->cookies == true) { |
|
110
|
|
|
curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file); |
|
111
|
|
|
} |
|
112
|
|
|
if ($this->cookies == true) { |
|
113
|
|
|
curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file); |
|
114
|
|
|
} |
|
115
|
|
|
curl_setopt($process, CURLOPT_ENCODING, $this->compression); |
|
116
|
|
|
curl_setopt($process, CURLOPT_TIMEOUT, 30); |
|
117
|
|
|
if ($this->proxy) { |
|
118
|
|
|
curl_setopt($process, CURLOPT_PROXY, $this->proxy); |
|
119
|
|
|
} |
|
120
|
|
|
curl_setopt($process, CURLOPT_POST, 1); |
|
121
|
|
|
curl_setopt($process, CURLOPT_POSTFIELDS, $data); |
|
122
|
|
|
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); |
|
123
|
|
|
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); |
|
124
|
|
|
$return = curl_exec($process); |
|
125
|
|
|
curl_close($process); |
|
126
|
|
|
return $return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
public function put($url = '', array $data) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
// 'http://example.com/api/conversations/cid123/status' |
|
134
|
|
|
$process = curl_init($url); |
|
135
|
|
|
|
|
136
|
|
|
curl_setopt($process, CURLOPT_RETURNTRANSFER, true); |
|
137
|
|
|
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "PUT"); |
|
138
|
|
|
$data = array("status" => 'R'); |
|
139
|
|
|
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data)); |
|
140
|
|
|
$response = curl_exec($process); |
|
141
|
|
|
if (false === $response) { |
|
142
|
|
|
$info = curl_getinfo($process); |
|
143
|
|
|
curl_close($process); |
|
144
|
|
|
die('error occured during curl exec. Additioanl info: ' . var_export($info)); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
curl_close($process); |
|
147
|
|
|
$decoded = json_decode($response); |
|
|
|
|
|
|
148
|
|
|
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { |
|
149
|
|
|
die('error occured: ' . $decoded->response->errormessage); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
echo 'response ok!'; |
|
152
|
|
|
return ($decoded->response); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function delete($value = '') |
|
156
|
|
|
{ |
|
157
|
|
|
// 'http://example.com/api/conversations/[CONVERSATION_ID]' |
|
158
|
|
|
$service_url = $value; |
|
159
|
|
|
$process = curl_init($service_url); |
|
160
|
|
|
curl_setopt($process, CURLOPT_RETURNTRANSFER, true); |
|
161
|
|
|
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE"); |
|
162
|
|
|
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($service_url)); |
|
163
|
|
|
$curl_response = curl_exec($process); |
|
164
|
|
|
if (false === $curl_response) { |
|
165
|
|
|
$info = curl_getinfo($process); |
|
166
|
|
|
curl_close($process); |
|
167
|
|
|
die('error occured during curl exec. Additioanl info: ' . var_export($info)); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
curl_close($process); |
|
170
|
|
|
$decoded = json_decode($curl_response); |
|
|
|
|
|
|
171
|
|
|
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { |
|
172
|
|
|
die('error occured: ' . $decoded->response->errormessage); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
echo 'response ok!'; |
|
175
|
|
|
return ($decoded->response); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
function error($error) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>"; |
|
181
|
|
|
die; |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|