1 | <?php |
||
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 | 12 | public function request($uri = '/', $method = 'GET', array $data = [], array $params = []) |
|
22 | { |
||
23 | 12 | 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 | 12 | $options = []; |
|
30 | $headers = [ |
||
31 | 12 | 'Accept: application/json', |
|
32 | 12 | 'User-Agent: MercadoPago PHP SDK v' . MercadoPago::VERSION, |
|
33 | 12 | ]; |
|
34 | |||
35 | 12 | if ($method !== self::METHOD_GET && !empty($data)) { |
|
36 | 8 | if (!empty($params['form'])) { |
|
37 | 4 | $options['form_data'] = $data; |
|
38 | 4 | array_push($headers, 'Content-Type: application/x-www-form-urlencoded'); |
|
39 | 4 | } else { |
|
40 | 4 | $options['json'] = $data; |
|
41 | 4 | array_push($headers, 'Content-Type: application/json'); |
|
42 | } |
||
43 | 8 | } else { |
|
44 | 4 | array_push($headers, 'Content-Type: application/json'); |
|
45 | } |
||
46 | |||
47 | 12 | if ($method === self::METHOD_GET && !empty($data)) { |
|
48 | 2 | $options['query'] = $data; |
|
49 | 2 | } |
|
50 | |||
51 | 12 | if (!empty($params['access_token'])) { |
|
52 | 8 | $options['query']['access_token'] = $params['access_token']; |
|
53 | 8 | } |
|
54 | |||
55 | 12 | $connect = curl_init(); |
|
56 | |||
57 | 12 | curl_setopt($connect, CURLOPT_RETURNTRANSFER, true); |
|
58 | 12 | curl_setopt($connect, CURLOPT_SSL_VERIFYPEER, true); |
|
59 | |||
60 | 12 | curl_setopt($connect, CURLOPT_CUSTOMREQUEST, $method); |
|
61 | 12 | curl_setopt($connect, CURLOPT_HTTPHEADER, $headers); |
|
62 | |||
63 | 12 | if (PHP_VERSION_ID < 50600) { |
|
64 | curl_setopt($connect, CURLOPT_CAINFO, default_ca_bundle()); |
||
65 | } |
||
66 | |||
67 | // Set parameters and url |
||
68 | 12 | if (isset($options['query']) && is_array($options['query']) && count($options['query']) > 0) { |
|
69 | 8 | $uri .= (strpos($uri, '?') === false) ? '?' : '&'; |
|
70 | 8 | $uri .= http_build_query($options['query'], '', '&'); |
|
71 | 8 | } |
|
72 | |||
73 | 12 | curl_setopt($connect, CURLOPT_URL, self::API_BASE_URL . $uri); |
|
74 | |||
75 | // Set data |
||
76 | 12 | if (isset($options['json'])) { |
|
77 | 4 | $options['json'] = json_encode($options['json']); |
|
78 | 4 | curl_setopt($connect, CURLOPT_POSTFIELDS, $options['json']); |
|
79 | |||
80 | 4 | if (function_exists('json_last_error')) { |
|
81 | 4 | $json_error = json_last_error(); |
|
82 | 4 | if ($json_error != JSON_ERROR_NONE) { |
|
83 | throw new InvalidArgumentException("JSON Error [{$json_error}] - Data: " . $options['json']); |
||
84 | } |
||
85 | 4 | } |
|
86 | 12 | } elseif (isset($options['form_data'])) { |
|
87 | 4 | curl_setopt($connect, CURLOPT_POSTFIELDS, http_build_query($options['form_data'], '', '&')); |
|
88 | 4 | } |
|
89 | |||
90 | 12 | $httpContent = curl_exec($connect); |
|
91 | 12 | $httpStatusCode = curl_getinfo($connect, CURLINFO_HTTP_CODE); |
|
92 | |||
93 | 12 | if ($httpContent === false) { |
|
94 | return new Response(500, '{"message": "Can\'t connect. ' . curl_error($connect) . '"}'); |
||
95 | } |
||
96 | |||
97 | 12 | $response = new Response($httpStatusCode, $httpContent); |
|
98 | |||
99 | 12 | curl_close($connect); |
|
100 | |||
101 | 12 | return $response; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 4 | public function get($uri = '/', array $data = [], array $params = []) |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 8 | public function post($uri = '/', array $data = [], array $params = []) |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function put($uri = '/', array $data = [], array $params = []) |
||
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | public function delete($uri = '/', array $data = [], array $params = []) |
||
135 | } |
||
136 |