1 | <?php |
||
13 | class Request |
||
14 | { |
||
15 | const DEFAULT_TOKEN = '1234'; |
||
16 | |||
17 | /** |
||
18 | * @var HttpClient |
||
19 | */ |
||
20 | protected $httpClient; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $loggedIn; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $filePathToUpload; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $csrfToken = ''; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $postFileData; |
||
42 | |||
43 | /** |
||
44 | * Common headers needed for every query. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $requestHeaders = [ |
||
49 | 'Accept: application/json, text/javascript, */*; q=0.01', |
||
50 | 'Accept-Language: en-US,en;q=0.5', |
||
51 | 'DNT: 1', |
||
52 | 'X-Pinterest-AppState: active', |
||
53 | 'X-NEW-APP: 1', |
||
54 | 'X-APP-VERSION: 71842d3', |
||
55 | 'X-Requested-With: XMLHttpRequest', |
||
56 | 'X-Pinterest-AppState:active', |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @param HttpClient $http |
||
61 | */ |
||
62 | public function __construct(HttpClient $http) |
||
67 | |||
68 | /** |
||
69 | * @param string $pathToFile |
||
70 | * @param string $url |
||
71 | * @return string |
||
72 | * @throws InvalidRequest |
||
73 | */ |
||
74 | public function upload($pathToFile, $url) |
||
80 | |||
81 | /** |
||
82 | * Executes request to Pinterest API. |
||
83 | * |
||
84 | * @param string $resourceUrl |
||
85 | * @param string $postString |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function exec($resourceUrl, $postString = '') |
||
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | */ |
||
109 | protected function getHttpHeaders() |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function hasToken() |
||
126 | |||
127 | /** |
||
128 | * Clear token information. |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | protected function clearToken() |
||
138 | |||
139 | /** |
||
140 | * Load cookies for this username and check if it was logged in. |
||
141 | * @param string $username |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function autoLogin($username) |
||
156 | |||
157 | /** |
||
158 | * @param string $username |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function loadCookiesFor($username) |
||
168 | |||
169 | /** |
||
170 | * Mark client as logged. |
||
171 | */ |
||
172 | public function login() |
||
182 | |||
183 | /** |
||
184 | * Mark client as logged out. |
||
185 | */ |
||
186 | public function logout() |
||
191 | |||
192 | /** |
||
193 | * Get current auth status. |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function isLoggedIn() |
||
201 | |||
202 | /** |
||
203 | * Create request string. |
||
204 | * |
||
205 | * @param array $data |
||
206 | * @param array|null $bookmarks |
||
207 | * @return string |
||
208 | */ |
||
209 | public static function createQuery(array $data = [], $bookmarks = null) |
||
221 | |||
222 | /** |
||
223 | * @param array|object $data |
||
224 | * @param array $bookmarks |
||
225 | * @return array |
||
226 | */ |
||
227 | public static function createRequestData(array $data = [], $bookmarks = []) |
||
246 | |||
247 | /** |
||
248 | * Cast all non-array values to strings |
||
249 | * |
||
250 | * @param $array |
||
251 | * @return array |
||
252 | */ |
||
253 | protected static function prepareArrayToJson(array $array) |
||
264 | |||
265 | /** |
||
266 | * Trying to execGet csrf token from cookies. |
||
267 | * |
||
268 | * @return $this |
||
269 | */ |
||
270 | protected function setTokenFromCookies() |
||
278 | |||
279 | /** |
||
280 | * @return array |
||
281 | */ |
||
282 | protected function getDefaultHttpHeaders() |
||
294 | |||
295 | /** |
||
296 | * If we are uploading file, we should build boundary form data. Otherwise |
||
297 | * it is simple urlencoded form. |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | protected function getContentTypeHeader() |
||
307 | |||
308 | /** |
||
309 | * @param string $delimiter |
||
310 | * @return $this |
||
311 | */ |
||
312 | protected function buildFilePostData($delimiter) |
||
324 | |||
325 | /** |
||
326 | * @return array |
||
327 | */ |
||
328 | protected function makeHeadersForUpload() |
||
338 | |||
339 | /** |
||
340 | * @return HttpClient |
||
341 | */ |
||
342 | public function getHttpClient() |
||
346 | |||
347 | /** |
||
348 | * @return string |
||
349 | */ |
||
350 | public function getCurrentUrl() |
||
354 | |||
355 | /** |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function dropCookies() |
||
365 | } |
||
366 |