1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MercadoPago\Http; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use MercadoPago\MercadoPago; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use function GuzzleHttp\default_ca_bundle; |
9
|
|
|
|
10
|
|
|
class CurlClient extends Client |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $uri |
14
|
|
|
* @param string $method |
15
|
|
|
* @param array $data |
16
|
|
|
* @param array $params |
17
|
|
|
* @return Response |
18
|
|
|
* @throws RuntimeException If curl is not installed. |
19
|
|
|
* @throws InvalidArgumentException If JSON payload is not valid. |
20
|
|
|
*/ |
21
|
18 |
|
public function request($uri = '/', $method = 'GET', array $data = [], array $params = []) |
22
|
|
|
{ |
23
|
18 |
|
if (!extension_loaded('curl')) { |
24
|
|
|
throw new RuntimeException( |
25
|
|
|
'cURL extension not found. You need to enable cURL in your php.ini or another configuration you have.' |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
18 |
|
$options = []; |
30
|
|
|
$headers = [ |
31
|
18 |
|
'Accept: application/json', |
32
|
18 |
|
'User-Agent: MercadoPago PHP SDK v' . MercadoPago::VERSION, |
33
|
|
|
]; |
34
|
|
|
|
35
|
18 |
|
if ($method !== self::METHOD_GET && !empty($data)) { |
36
|
12 |
|
if (!empty($params['form'])) { |
37
|
6 |
|
$options['form_data'] = $data; |
38
|
6 |
|
array_push($headers, 'Content-Type: application/x-www-form-urlencoded'); |
39
|
|
|
} else { |
40
|
6 |
|
$options['json'] = $data; |
41
|
12 |
|
array_push($headers, 'Content-Type: application/json'); |
42
|
|
|
} |
43
|
|
|
} else { |
44
|
6 |
|
array_push($headers, 'Content-Type: application/json'); |
45
|
|
|
} |
46
|
|
|
|
47
|
18 |
|
if ($method === self::METHOD_GET && !empty($data)) { |
48
|
3 |
|
$options['query'] = $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
18 |
|
if (!empty($params['access_token'])) { |
52
|
12 |
|
$options['query']['access_token'] = $params['access_token']; |
53
|
|
|
} |
54
|
|
|
|
55
|
18 |
|
$connect = curl_init(); |
56
|
|
|
|
57
|
18 |
|
curl_setopt($connect, CURLOPT_RETURNTRANSFER, true); |
58
|
18 |
|
curl_setopt($connect, CURLOPT_SSL_VERIFYPEER, true); |
59
|
|
|
|
60
|
18 |
|
curl_setopt($connect, CURLOPT_CUSTOMREQUEST, $method); |
61
|
18 |
|
curl_setopt($connect, CURLOPT_HTTPHEADER, $headers); |
62
|
|
|
|
63
|
18 |
|
if (PHP_VERSION_ID < 50600) { |
64
|
|
|
curl_setopt($connect, CURLOPT_CAINFO, default_ca_bundle()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Set parameters and url |
68
|
18 |
|
if (isset($options['query']) && is_array($options['query']) && count($options['query']) > 0) { |
69
|
12 |
|
$uri .= (strpos($uri, '?') === false) ? '?' : '&'; |
70
|
12 |
|
$uri .= http_build_query($options['query'], '', '&'); |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
curl_setopt($connect, CURLOPT_URL, self::API_BASE_URL . $uri); |
74
|
|
|
|
75
|
|
|
// Set data |
76
|
18 |
|
if (isset($options['json'])) { |
77
|
6 |
|
$options['json'] = json_encode($options['json']); |
78
|
6 |
|
curl_setopt($connect, CURLOPT_POSTFIELDS, $options['json']); |
79
|
|
|
|
80
|
6 |
|
if (function_exists('json_last_error')) { |
81
|
6 |
|
$json_error = json_last_error(); |
82
|
6 |
|
if ($json_error != JSON_ERROR_NONE) { |
83
|
6 |
|
throw new InvalidArgumentException("JSON Error [{$json_error}] - Data: " . $options['json']); |
84
|
|
|
} |
85
|
|
|
} |
86
|
12 |
|
} elseif (isset($options['form_data'])) { |
87
|
6 |
|
curl_setopt($connect, CURLOPT_POSTFIELDS, http_build_query($options['form_data'], '', '&')); |
88
|
|
|
} |
89
|
|
|
|
90
|
18 |
|
$httpContent = curl_exec($connect); |
91
|
18 |
|
$httpStatusCode = curl_getinfo($connect, CURLINFO_HTTP_CODE); |
92
|
|
|
|
93
|
18 |
|
if ($httpContent === false) { |
94
|
|
|
return new Response(500, '{"message": "Can\'t connect. ' . curl_error($connect) . '"}'); |
95
|
|
|
} |
96
|
|
|
|
97
|
18 |
|
$response = new Response($httpStatusCode, $httpContent); |
98
|
|
|
|
99
|
18 |
|
curl_close($connect); |
100
|
|
|
|
101
|
18 |
|
return $response; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
6 |
|
public function get($uri = '/', array $data = [], array $params = []) |
108
|
|
|
{ |
109
|
6 |
|
return $this->request($uri, self::METHOD_GET, $data, $params); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
12 |
|
public function post($uri = '/', array $data = [], array $params = []) |
116
|
|
|
{ |
117
|
12 |
|
return $this->request($uri, self::METHOD_POST, $data, $params); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
public function put($uri = '/', array $data = [], array $params = []) |
124
|
|
|
{ |
125
|
|
|
return $this->request($uri, self::METHOD_PUT, $data, $params); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
|
|
public function delete($uri = '/', array $data = [], array $params = []) |
132
|
|
|
{ |
133
|
|
|
return $this->request($uri, self::METHOD_DELETE, $data, $params); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|