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: 831c928', |
||
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 | /** |
||
121 | * Clear token information. |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | protected function clearToken() |
||
131 | |||
132 | /** |
||
133 | * Load cookies for this username and check if it was logged in. |
||
134 | * @param string $username |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function autoLogin($username) |
||
149 | |||
150 | /** |
||
151 | * @param string $username |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function loadCookiesFor($username) |
||
160 | |||
161 | /** |
||
162 | * Mark client as logged. |
||
163 | */ |
||
164 | public function login() |
||
174 | |||
175 | /** |
||
176 | * Mark client as logged out. |
||
177 | */ |
||
178 | public function logout() |
||
183 | |||
184 | /** |
||
185 | * Get current auth status. |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function isLoggedIn() |
||
193 | |||
194 | /** |
||
195 | * Create request string. |
||
196 | * |
||
197 | * @param array $data |
||
198 | * @param array|null $bookmarks |
||
199 | * @return string |
||
200 | */ |
||
201 | public static function createQuery(array $data = [], $bookmarks = null) |
||
213 | |||
214 | /** |
||
215 | * @param array|object $data |
||
216 | * @param array $bookmarks |
||
217 | * @return array |
||
218 | */ |
||
219 | public static function createRequestData(array $data = [], $bookmarks = []) |
||
238 | |||
239 | /** |
||
240 | * Cast all non-array values to strings |
||
241 | * |
||
242 | * @param $array |
||
243 | * @return array |
||
244 | */ |
||
245 | protected static function prepareArrayToJson(array $array) |
||
256 | |||
257 | /** |
||
258 | * Trying to execGet csrf token from cookies. |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | protected function setTokenFromCookies() |
||
270 | |||
271 | /** |
||
272 | * @return array |
||
273 | */ |
||
274 | protected function getDefaultHttpHeaders() |
||
286 | |||
287 | /** |
||
288 | * If we are uploading file, we should build boundary form data. Otherwise |
||
289 | * it is simple urlencoded form. |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | protected function getContentTypeHeader() |
||
299 | |||
300 | /** |
||
301 | * @param string $delimiter |
||
302 | * @return $this |
||
303 | */ |
||
304 | protected function buildFilePostData($delimiter) |
||
316 | |||
317 | /** |
||
318 | * @return array |
||
319 | */ |
||
320 | protected function makeHeadersForUpload() |
||
330 | |||
331 | /** |
||
332 | * @return HttpClient |
||
333 | */ |
||
334 | public function getHttpClient() |
||
338 | |||
339 | /** |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getCurrentUrl() |
||
346 | |||
347 | /** |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function dropCookies() |
||
357 | } |
||
358 |