1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Classes\Http; |
4
|
|
|
|
5
|
|
|
final class CurlOnlyPostHttpClient implements OnlyPostHttpClientInterface |
6
|
|
|
{ |
7
|
|
|
const CLIENT = 'IpagSdkPhp'; |
8
|
|
|
|
9
|
|
|
private $httpHeaders; |
10
|
|
|
private $httpPostFields; |
11
|
|
|
private $userAgent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $user; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $password; |
22
|
|
|
|
23
|
|
|
public function __invoke($endpoint, array $headers = [], array $fields = []) |
24
|
|
|
{ |
25
|
|
|
$this->httpHeaders = $this->formatToHttpHeaders($headers); |
26
|
|
|
$this->httpPostFields = $this->formatToHttpPostFields($fields); |
27
|
|
|
$this->userAgent = sprintf('%s (+https://github.com/jhernandes/ipag-sdk-php/)', self::CLIENT); |
28
|
|
|
|
29
|
|
|
return $this->post($endpoint); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function post($endpoint) |
33
|
|
|
{ |
34
|
|
|
$curl = curl_init(); |
35
|
|
|
curl_setopt($curl, CURLOPT_URL, $endpoint); |
36
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, "{$this->user}:{$this->password}"); |
37
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->httpHeaders); |
38
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->httpPostFields); |
39
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
40
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
41
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
42
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
43
|
|
|
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); |
44
|
|
|
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
45
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent); |
46
|
|
|
|
47
|
|
|
$response = curl_exec($curl); |
48
|
|
|
|
49
|
|
|
$this->curlHasError($curl); |
50
|
|
|
|
51
|
|
|
curl_close($curl); |
52
|
|
|
|
53
|
|
|
return $response; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function formatToHttpHeaders(array $headers = []) |
57
|
|
|
{ |
58
|
|
|
return array_map( |
59
|
|
|
function ($name, $value) { |
60
|
|
|
return "{$name}: {$value}"; |
61
|
|
|
}, |
62
|
|
|
array_keys($headers), |
63
|
|
|
array_values($headers) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function formatToHttpPostFields(array $fields = []) |
68
|
|
|
{ |
69
|
|
|
$formattedFields = ''; |
70
|
|
|
foreach ($fields as $name => $value) { |
71
|
|
|
$formattedFields .= "{$name}={$value}&"; |
72
|
|
|
} |
73
|
|
|
rtrim($formattedFields, '&'); |
74
|
|
|
|
75
|
|
|
return $formattedFields; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function curlHasError($curl) |
79
|
|
|
{ |
80
|
|
|
if (curl_errno($curl)) { |
81
|
|
|
throw new \Exception('Curl error: '.curl_error($curl)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getUser() |
89
|
|
|
{ |
90
|
|
|
return $this->user; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $user |
95
|
|
|
*/ |
96
|
|
|
public function setUser($user) |
97
|
|
|
{ |
98
|
|
|
$this->user = $user; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getPassword() |
107
|
|
|
{ |
108
|
|
|
return $this->password; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $password |
113
|
|
|
*/ |
114
|
|
|
public function setPassword($password) |
115
|
|
|
{ |
116
|
|
|
$this->password = $password; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|