1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 Dirk Groenen |
4
|
|
|
* |
5
|
|
|
* (c) Dirk Groenen <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DirkGroenen\Pinterest\Transport; |
12
|
|
|
|
13
|
|
|
use DirkGroenen\Pinterest\Utils\CurlBuilder; |
14
|
|
|
use DirkGroenen\Pinterest\Exceptions\PinterestException; |
15
|
|
|
use DirkGroenen\Pinterest\Exceptions\CurlException; |
16
|
|
|
|
17
|
|
|
class Request { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Host to make the calls to |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $host = "https://api.pinterest.com/v1/"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Access token |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $access_token = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Instance of the CurlBuilder class |
35
|
|
|
* |
36
|
|
|
* @var CurlBuilder |
37
|
|
|
*/ |
38
|
|
|
private $curlbuilder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Array with the headers from the last request |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $headers; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* |
50
|
|
|
* @param CurlBuilder $curlbuilder |
51
|
|
|
*/ |
52
|
39 |
|
public function __construct(CurlBuilder $curlbuilder) |
53
|
|
|
{ |
54
|
39 |
|
$this->curlbuilder = $curlbuilder; |
55
|
39 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the access token |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* @param string $token |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
39 |
|
public function setAccessToken($token) |
65
|
|
|
{ |
66
|
39 |
|
$this->access_token = $token; |
67
|
39 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Make a get request to the given endpoint |
71
|
|
|
* |
72
|
|
|
* @access public |
73
|
|
|
* @param string $endpoint |
74
|
|
|
* @param array $parameters |
75
|
|
|
* @return Response |
76
|
|
|
*/ |
77
|
25 |
View Code Duplication |
public function get($endpoint, array $parameters = array()) |
|
|
|
|
78
|
|
|
{ |
79
|
25 |
|
if (!empty($parameters)) { |
80
|
5 |
|
$path = sprintf("%s/?%s", $endpoint, http_build_query($parameters)); |
81
|
5 |
|
} else { |
82
|
20 |
|
$path = $endpoint; |
83
|
|
|
} |
84
|
|
|
|
85
|
25 |
|
return $this->execute("GET", sprintf("%s%s", $this->host, $path)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Make a post request to the given endpoint |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* @param string $endpoint |
93
|
|
|
* @param array $parameters |
94
|
|
|
* @return Response |
95
|
|
|
*/ |
96
|
4 |
|
public function post($endpoint, array $parameters = array()) |
97
|
|
|
{ |
98
|
4 |
|
return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Make a delete request to the given endpoint |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* @param string $endpoint |
106
|
|
|
* @param array $parameters |
107
|
|
|
* @return Response |
108
|
|
|
*/ |
109
|
5 |
|
public function delete($endpoint, array $parameters = array()) |
110
|
|
|
{ |
111
|
5 |
|
return $this->execute("DELETE", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Make an update request to the given endpoint |
116
|
|
|
* |
117
|
|
|
* @access public |
118
|
|
|
* @param string $endpoint |
119
|
|
|
* @param array $parameters |
120
|
|
|
* @param array $queryparameters |
121
|
|
|
* @return Response |
122
|
|
|
*/ |
123
|
2 |
View Code Duplication |
public function update($endpoint, array $parameters = array(), array $queryparameters = array()) |
|
|
|
|
124
|
|
|
{ |
125
|
2 |
|
if (!empty($queryparameters)) { |
126
|
|
|
$path = sprintf("%s/?%s", $endpoint, http_build_query($queryparameters)); |
127
|
|
|
} else { |
128
|
2 |
|
$path = $endpoint; |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return the headers from the last request |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
2 |
|
public function getHeaders() |
140
|
|
|
{ |
141
|
2 |
|
return $this->headers; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Execute the http request |
146
|
|
|
* |
147
|
|
|
* @access public |
148
|
|
|
* @param string $method |
149
|
|
|
* @param string $apiCall |
150
|
|
|
* @param array $parameters |
151
|
|
|
* @param array $headers |
152
|
|
|
* @return Response |
153
|
|
|
*/ |
154
|
35 |
|
public function execute($method, $apiCall, array $parameters = array(), $headers = array()) |
155
|
|
|
{ |
156
|
|
|
// Check if the access token needs to be added |
157
|
35 |
|
if ($this->access_token != null) { |
158
|
35 |
|
$headers = array_merge($headers, array( |
159
|
35 |
|
"Authorization: Bearer " . $this->access_token, |
160
|
|
|
//"Content-type: application/x-www-form-urlencoded; charset=UTF-8", |
161
|
|
|
"Content-Type:multipart/form-data" |
162
|
35 |
|
)); |
163
|
35 |
|
} |
164
|
|
|
|
165
|
|
|
// Setup CURL |
166
|
35 |
|
$ch = $this->curlbuilder->create(); |
167
|
|
|
|
168
|
|
|
// Set default options |
169
|
35 |
|
$ch->setOptions(array( |
170
|
35 |
|
CURLOPT_URL => $apiCall, |
171
|
35 |
|
CURLOPT_HTTPHEADER => $headers, |
172
|
35 |
|
CURLOPT_CONNECTTIMEOUT => 20, |
173
|
35 |
|
CURLOPT_TIMEOUT => 90, |
174
|
35 |
|
CURLOPT_RETURNTRANSFER => true, |
175
|
35 |
|
CURLOPT_SSL_VERIFYPEER => false, |
176
|
35 |
|
CURLOPT_SSL_VERIFYHOST => false, |
177
|
35 |
|
CURLOPT_HEADER => false, |
178
|
35 |
|
CURLINFO_HEADER_OUT => true |
179
|
35 |
|
)); |
180
|
|
|
|
181
|
|
|
switch ($method) { |
182
|
35 |
|
case 'POST': |
183
|
3 |
|
$ch->setOptions(array( |
184
|
3 |
|
CURLOPT_CUSTOMREQUEST => "POST", |
185
|
3 |
|
CURLOPT_POST => count($parameters), |
186
|
3 |
|
CURLOPT_POSTFIELDS => ($parameters) |
187
|
3 |
|
)); |
188
|
|
|
|
189
|
3 |
|
if (!class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) { |
190
|
|
|
$ch->setOption(CURLOPT_SAFE_UPLOAD, false); |
191
|
|
|
} |
192
|
3 |
|
elseif (class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) { |
193
|
|
|
$ch->setOption(CURLOPT_SAFE_UPLOAD, true); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
3 |
|
break; |
197
|
32 |
|
case 'DELETE': |
198
|
5 |
|
$ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE"); |
199
|
5 |
|
break; |
200
|
27 |
|
case 'PATCH': |
201
|
2 |
|
$ch->setOptions(array( |
202
|
2 |
|
CURLOPT_CUSTOMREQUEST => "PATCH", |
203
|
2 |
|
CURLOPT_POST => count($parameters), |
204
|
2 |
|
CURLOPT_POSTFIELDS => http_build_query($parameters) |
205
|
2 |
|
)); |
206
|
2 |
|
break; |
207
|
25 |
|
default: |
208
|
25 |
|
$ch->setOption(CURLOPT_CUSTOMREQUEST, "GET"); |
209
|
25 |
|
break; |
210
|
25 |
|
} |
211
|
|
|
|
212
|
|
|
// Execute request and catch response |
213
|
35 |
|
$response_data = $ch->execute(); |
214
|
|
|
|
215
|
35 |
|
if ($response_data === false && !$ch->hasErrors()) { |
216
|
|
|
throw new CurlException("Error: Curl request failed"); |
217
|
|
|
} |
218
|
35 |
|
else if($ch->hasErrors()) { |
219
|
|
|
throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Initiate the response |
223
|
35 |
|
$response = new Response($response_data, $ch); |
|
|
|
|
224
|
|
|
|
225
|
|
|
// Check the response code |
226
|
35 |
|
if ($response->getResponseCode() >= 400) { |
227
|
1 |
|
throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->getMessage(), $response->getResponseCode()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// Get headers from last request |
231
|
34 |
|
$this->headers = $ch->getHeaders(); |
232
|
|
|
|
233
|
|
|
// Close curl resource |
234
|
34 |
|
$ch->close(); |
235
|
|
|
|
236
|
|
|
// Return the response |
237
|
34 |
|
return $response; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.