1 | <?php |
||
20 | class Request |
||
21 | { |
||
22 | const COOKIE_NAME = 'pinterest_cookie'; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $userAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'; |
||
28 | |||
29 | /** |
||
30 | * @var HttpInterface |
||
31 | */ |
||
32 | protected $http; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $loggedIn; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $cookieJar; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $options; |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $filePathToUpload; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $csrfToken = ''; |
||
59 | |||
60 | /** |
||
61 | * Common headers needed for every query. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $requestHeaders = [ |
||
66 | 'Accept: application/json, text/javascript, */*; q=0.01', |
||
67 | 'Accept-Language: en-US,en;q=0.5', |
||
68 | 'DNT: 1', |
||
69 | 'X-Pinterest-AppState: active', |
||
70 | 'X-NEW-APP: 1', |
||
71 | 'X-APP-VERSION: 04cf8cc', |
||
72 | 'X-Requested-With: XMLHttpRequest', |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $postFileData; |
||
79 | |||
80 | /** |
||
81 | * @param HttpInterface $http |
||
82 | */ |
||
83 | public function __construct(HttpInterface $http) |
||
89 | |||
90 | /** |
||
91 | * @param string $pathToFile |
||
92 | * @param string $url |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public function upload($pathToFile, $url) |
||
101 | |||
102 | /** |
||
103 | * Executes request to Pinterest API. |
||
104 | * |
||
105 | * @param string $resourceUrl |
||
106 | * @param string $postString |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function exec($resourceUrl, $postString = '') |
||
119 | |||
120 | /** |
||
121 | * Adds necessary curl options for query. |
||
122 | * |
||
123 | * @param string $postString POST query string |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | protected function makeHttpOptions($postString = '') |
||
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | protected function setDefaultHttpOptions() |
||
160 | |||
161 | /** |
||
162 | * @param array $options |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | protected function addDefaultCsrfInfo($options) |
||
173 | |||
174 | /** |
||
175 | * Clear token information. |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function clearToken() |
||
185 | |||
186 | /** |
||
187 | * Mark api as logged. |
||
188 | * @return $this |
||
189 | * @throws AuthException |
||
190 | */ |
||
191 | public function login() |
||
196 | |||
197 | public function logout() |
||
202 | |||
203 | /** |
||
204 | * Get log status. |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function isLoggedIn() |
||
212 | |||
213 | /** |
||
214 | * @param $userAgent |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function setUserAgent($userAgent) |
||
225 | |||
226 | /** |
||
227 | * Create request string. |
||
228 | * |
||
229 | * @param array $data |
||
230 | * @param array $bookmarks |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public static function createQuery(array $data = [], $bookmarks = []) |
||
241 | |||
242 | /** |
||
243 | * @param array|object $data |
||
244 | * @param array $bookmarks |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | public static function createRequestData(array $data = [], $bookmarks = []) |
||
265 | |||
266 | public function setTokenFromCookies() |
||
275 | |||
276 | /** |
||
277 | * @return array |
||
278 | */ |
||
279 | protected function getDefaultHttpHeaders() |
||
288 | |||
289 | /** |
||
290 | * If we are uploading file, we should build boundary form data. Otherwise |
||
291 | * it is simple urlencoded form. |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | protected function getContentTypeHeader() |
||
301 | |||
302 | /** |
||
303 | * @param string $delimiter |
||
304 | * @return $this |
||
305 | */ |
||
306 | protected function buildFilePostData($delimiter) |
||
318 | |||
319 | protected function makeHeadersForUpload() |
||
329 | } |
||
330 |