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
|
|
|
|
16
|
|
|
class Request { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Host to make the calls to |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $host = "https://api.pinterest.com/v1/"; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Access token |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $access_token = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Instance of the CurlBuilder class |
34
|
|
|
* |
35
|
|
|
* @var CurlBuilder |
36
|
|
|
*/ |
37
|
|
|
private $curlbuilder; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Array with the headers from the last request |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $headers; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor |
48
|
|
|
* |
49
|
|
|
* @param CurlBuilder $curlbuilder |
50
|
|
|
*/ |
51
|
37 |
|
public function __construct(CurlBuilder $curlbuilder) |
52
|
|
|
{ |
53
|
37 |
|
$this->curlbuilder = $curlbuilder; |
54
|
37 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the access token |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* @param string $token |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
37 |
|
public function setAccessToken($token) |
64
|
|
|
{ |
65
|
37 |
|
$this->access_token = $token; |
66
|
37 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Make a get request to the given endpoint |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @param string $endpoint |
73
|
|
|
* @param array $parameters |
74
|
|
|
* @return Response |
75
|
|
|
*/ |
76
|
25 |
|
public function get($endpoint, array $parameters = array()) |
77
|
|
|
{ |
78
|
25 |
|
if (!empty($parameters)) { |
79
|
5 |
|
$path = sprintf("%s/?%s", $endpoint, http_build_query($parameters)); |
80
|
5 |
|
} else { |
81
|
20 |
|
$path = $endpoint; |
82
|
|
|
} |
83
|
|
|
|
84
|
25 |
|
return $this->execute("GET", sprintf("%s%s", $this->host, $path)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Make a post request to the given endpoint |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
* @param string $endpoint |
92
|
|
|
* @param array $parameters |
93
|
|
|
* @return Response |
94
|
|
|
*/ |
95
|
3 |
|
public function post($endpoint, array $parameters = array()) |
96
|
|
|
{ |
97
|
3 |
|
return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Make a delete request to the given endpoint |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
* @param string $endpoint |
105
|
|
|
* @param array $parameters |
106
|
|
|
* @return Response |
107
|
|
|
*/ |
108
|
5 |
|
public function delete($endpoint, array $parameters = array()) |
109
|
|
|
{ |
110
|
5 |
|
return $this->execute("DELETE", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Make an update request to the given endpoint |
115
|
|
|
* |
116
|
|
|
* @access public |
117
|
|
|
* @param string $endpoint |
118
|
|
|
* @param array $parameters |
119
|
|
|
* @return Response |
120
|
|
|
*/ |
121
|
|
|
public function update($endpoint, array $parameters = array()) |
122
|
|
|
{ |
123
|
|
|
return $this->execute("PATCH", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the headers from the last request |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
2 |
|
public function getHeaders() |
132
|
|
|
{ |
133
|
2 |
|
return $this->headers; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Execute the http request |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
* @param string $method |
141
|
|
|
* @param string $apiCall |
142
|
|
|
* @param array $parameters |
143
|
|
|
* @param array $headers |
144
|
|
|
* @return Response |
145
|
|
|
*/ |
146
|
33 |
|
public function execute($method, $apiCall, array $parameters = array(), $headers = array()) |
147
|
|
|
{ |
148
|
|
|
// Check if the access token needs to be added |
149
|
33 |
|
if ($this->access_token != null) { |
150
|
33 |
|
$headers = array_merge($headers, array( |
151
|
33 |
|
"Authorization: Bearer " . $this->access_token, |
152
|
33 |
|
"Content-ype: multipart/form-data", |
153
|
33 |
|
)); |
154
|
33 |
|
} |
155
|
|
|
|
156
|
|
|
// Setup CURL |
157
|
33 |
|
$ch = $this->curlbuilder->create(); |
158
|
|
|
|
159
|
|
|
// Set default options |
160
|
33 |
|
$ch->setOptions(array( |
161
|
33 |
|
CURLOPT_URL => $apiCall, |
162
|
33 |
|
CURLOPT_HTTPHEADER => $headers, |
163
|
33 |
|
CURLOPT_CONNECTTIMEOUT => 20, |
164
|
33 |
|
CURLOPT_TIMEOUT => 90, |
165
|
33 |
|
CURLOPT_RETURNTRANSFER => true, |
166
|
33 |
|
CURLOPT_SSL_VERIFYPEER => false, |
167
|
33 |
|
CURLOPT_SSL_VERIFYHOST => false, |
168
|
33 |
|
CURLOPT_HEADER => false, |
169
|
33 |
|
CURLINFO_HEADER_OUT => true |
170
|
33 |
|
)); |
171
|
|
|
|
172
|
|
|
switch ($method) { |
173
|
33 |
|
case 'POST': |
174
|
3 |
|
$ch->setOptions(array( |
175
|
3 |
|
CURLOPT_CUSTOMREQUEST => 'POST', |
176
|
3 |
|
CURLOPT_POST => count($parameters), |
177
|
3 |
|
CURLOPT_POSTFIELDS => $parameters |
178
|
3 |
|
)); |
179
|
|
|
|
180
|
3 |
|
if (!class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) { |
181
|
|
|
$ch->setOption(CURLOPT_SAFE_UPLOAD, false); |
182
|
|
|
} |
183
|
|
|
|
184
|
3 |
|
break; |
185
|
30 |
|
case 'DELETE': |
186
|
5 |
|
$ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE"); |
187
|
5 |
|
break; |
188
|
25 |
|
case 'PATCH': |
189
|
|
|
$ch->setOption(CURLOPT_CUSTOMREQUEST, "PATCH"); |
190
|
|
|
break; |
191
|
25 |
|
default: |
192
|
25 |
|
$ch->setOption(CURLOPT_CUSTOMREQUEST, "GET"); |
193
|
25 |
|
break; |
194
|
25 |
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
// Execute request and catch response |
198
|
33 |
|
$response_data = $ch->execute(); |
199
|
|
|
|
200
|
|
|
// Check if we have a valid response |
201
|
33 |
|
if (!$response_data || $ch->hasErrors()) { |
|
|
|
|
202
|
|
|
throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Initiate the response |
206
|
33 |
|
$response = new Response($response_data, $ch); |
207
|
|
|
|
208
|
|
|
// Check the response code |
209
|
33 |
|
if ($response->getResponseCode() >= 400) { |
210
|
1 |
|
throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->message, $response->getResponseCode()); |
211
|
|
|
} |
212
|
32 |
|
$this->headers = $ch->getHeaders(); |
213
|
|
|
|
214
|
|
|
// Close curl resource |
215
|
32 |
|
$ch->close(); |
216
|
|
|
|
217
|
|
|
// Return the response |
218
|
32 |
|
return $response; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: