-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.php
More file actions
87 lines (75 loc) · 2.14 KB
/
Http.php
File metadata and controls
87 lines (75 loc) · 2.14 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
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Http class CURL wrapper
* User: Stepanovit
* Date: 16.03.2017
*
* VERSION = 0.0.1
*
*/
class Http{
private $curl;
public $url;
public $method;
public $request;
public $response;
public $info;
public function __construct($url, $method = 'GET', $data = '', $options = array()){
$this->url = $url;
$this->method = $method;
$this->request = $data;
}
public static function get($url){
$http = new Http($url);
$http->process();
return $http;
}
public static function post($url, $data){
$http = new Http($url, 'POST', $data);
$http->process();
return $http;
}
public static function delete($url, $data){
$http = new Http($url, 'DELETE', $data);
$http->process();
return $http;
}
public static function put($url, $data){
$http = new Http($url, 'PUT', $data);
$http->process();
return $http;
}
public static function patch($url, $data){
$http = new Http($url, 'PATCH', $data);
$http->process();
return $http;
}
public function process(){
$this->curl = curl_init($this->url);
curl_setopt($this->curl, CURLOPT_URL, $this->url);
$this->set_options();
$this->exec();
}
public function json(){
return json_decode($this->response);
}
public function xml(){
$xml = new SimpleXMLElement($this->response);
return json_decode(json_encode($xml), FALSE);
}
private function set_options(){
if($this->method !== 'GET'){
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $this->method);
}
if($this->method == "PUT" || $this->method == "PATCH" || ($this->method == "POST") || ($this->method == "DELETE")) {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->request);
}
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
}
private function exec(){
$result = curl_exec($this->curl);
$this->info = curl_getinfo($this->curl);
curl_close($this->curl);
$this->response = $result;
}
}