1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Tharanga Kothalawala <[email protected]> |
4
|
|
|
* @date 30-12-2018 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace TSK\SSO\Http; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
* @internal |
12
|
|
|
* @package TSK\SSO\Storage |
13
|
|
|
* |
14
|
|
|
* This can send GET & POST requests. @todo: Add Guzzle |
15
|
|
|
*/ |
16
|
|
|
class CurlRequest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var resource |
20
|
|
|
*/ |
21
|
|
|
private $curl; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->curl = curl_init(); |
26
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
27
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); |
28
|
|
|
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); |
29
|
|
|
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 120); |
30
|
|
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function __destruct() |
34
|
|
|
{ |
35
|
|
|
if (!empty($this->curl)) { |
36
|
|
|
curl_close($this->curl); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* makes GET request to a given endpoint |
42
|
|
|
* |
43
|
|
|
* @param string $url external url |
44
|
|
|
* @param array $headers http headers if any [optional] |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function get($url, array $headers = array()) |
48
|
|
|
{ |
49
|
|
|
curl_setopt($this->curl, CURLOPT_POST, false); |
50
|
|
|
|
51
|
|
|
return $this->request($url, $headers); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* makes POST request to a given endpoint |
56
|
|
|
* |
57
|
|
|
* @param string $url external url |
58
|
|
|
* @param array $data [optional] data to post |
59
|
|
|
* @param array $headers [optional] http headers if any |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function post($url, array $data = array(), array $headers = array()) |
63
|
|
|
{ |
64
|
|
|
if (!empty($data)) { |
65
|
|
|
curl_setopt($this->curl, CURLOPT_POST, count($data)); |
66
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->request($url, $headers); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* makes POST request to a given endpoint using URL encoded data |
74
|
|
|
* |
75
|
|
|
* @param string $url external url |
76
|
|
|
* @param string $rawData [optional] data to post |
77
|
|
|
* @param array $headers [optional] http headers if any |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function postUrlEncoded($url, $urlEncodedData = null, array $headers = array()) |
81
|
|
|
{ |
82
|
|
|
if (!empty($urlEncodedData)) { |
83
|
|
|
curl_setopt($this->curl, CURLOPT_POST, strlen($urlEncodedData)); |
84
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $urlEncodedData); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
88
|
|
|
|
89
|
|
|
return $this->request($url, $headers); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function request($url, array $headers) |
93
|
|
|
{ |
94
|
|
|
curl_setopt($this->curl, CURLOPT_URL, $url); |
95
|
|
|
curl_setopt($this->curl, CURLOPT_REFERER, $url); |
96
|
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
97
|
|
|
|
98
|
|
|
if (!empty($headers)) { |
99
|
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return curl_exec($this->curl); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|