1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
+------------------------------------------------------------------------+ |
4
|
|
|
| Plinker-RPC PHP | |
5
|
|
|
+------------------------------------------------------------------------+ |
6
|
|
|
| Copyright (c)2017-2018 (https://github.com/plinker-rpc/core) | |
7
|
|
|
+------------------------------------------------------------------------+ |
8
|
|
|
| This source file is subject to MIT License | |
9
|
|
|
| that is bundled with this package in the file LICENSE. | |
10
|
|
|
| | |
11
|
|
|
| If you did not receive a copy of the license and are unable to | |
12
|
|
|
| obtain it through the world-wide-web, please send an email | |
13
|
|
|
| to [email protected] so we can send you a copy immediately. | |
14
|
|
|
+------------------------------------------------------------------------+ |
15
|
|
|
| Authors: Lawrence Cherone <[email protected]> | |
16
|
|
|
+------------------------------------------------------------------------+ |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Plinker\Core\Lib; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Plinker\Core\Lib\Curl |
23
|
|
|
*/ |
24
|
|
|
final class Curl |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private $options; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class construct |
38
|
|
|
* |
39
|
|
|
* @param array $config - config array which holds object configuration |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
1 |
|
public function __construct(array $config = []) |
43
|
|
|
{ |
44
|
|
|
// |
45
|
1 |
|
$this->config = $config; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
1 |
|
private function setOptions() |
52
|
|
|
{ |
53
|
|
|
// |
54
|
1 |
|
$this->options = [ |
55
|
1 |
|
CURLOPT_FAILONERROR => true, |
56
|
1 |
|
CURLOPT_FOLLOWLOCATION => true, |
57
|
1 |
|
CURLOPT_RETURNTRANSFER => true, |
58
|
1 |
|
CURLOPT_SSL_VERIFYPEER => false, |
59
|
1 |
|
CURLOPT_SSL_VERIFYHOST => false, |
60
|
1 |
|
CURLOPT_ENCODING => "gzip", |
61
|
1 |
|
CURLOPT_HTTPHEADER => [ |
62
|
|
|
"Content-Type: application/json" |
63
|
|
|
] |
64
|
|
|
]; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* POST |
69
|
|
|
* |
70
|
|
|
* @param string $url - url of the plinker server |
71
|
|
|
* @param array $parameters - post parameters |
72
|
|
|
*/ |
73
|
1 |
|
public function post($url, $parameters = [], $headers = []) |
74
|
|
|
{ |
75
|
|
|
// |
76
|
1 |
|
$this->setOptions(); |
77
|
|
|
|
78
|
|
|
// |
79
|
1 |
|
$parameters = json_encode($parameters); |
80
|
1 |
|
$parameters = gzdeflate($parameters, 9); |
81
|
|
|
|
82
|
|
|
// |
83
|
1 |
|
$curl = curl_init($url); |
84
|
|
|
|
85
|
|
|
// |
86
|
1 |
|
$this->options[CURLOPT_POST] = true; |
87
|
1 |
|
$this->options[CURLOPT_POSTFIELDS] = $parameters; |
88
|
|
|
|
89
|
|
|
// set request headers |
90
|
1 |
|
if (!empty($headers)) { |
91
|
1 |
|
foreach ($headers as $header) { |
92
|
1 |
|
$this->options[CURLOPT_HTTPHEADER][] = $header; |
93
|
|
|
} |
94
|
1 |
|
$headers = []; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// |
98
|
1 |
|
curl_setopt_array($curl, $this->options); |
99
|
|
|
|
100
|
|
|
// |
101
|
1 |
|
$body = curl_exec($curl); |
102
|
|
|
|
103
|
|
|
// |
104
|
|
|
$return = [ |
105
|
1 |
|
'body' => $body, |
106
|
1 |
|
'code' => curl_getinfo($curl, CURLINFO_HTTP_CODE), |
107
|
1 |
|
'error' => (curl_error($curl) ? curl_error($curl) : null) |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
// |
111
|
1 |
|
curl_close($curl); |
112
|
|
|
|
113
|
|
|
// |
114
|
1 |
|
return $return; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|