1 | <?php |
||
20 | class Request |
||
21 | { |
||
22 | const COOKIE_NAME = 'pinterest_cookie'; |
||
23 | |||
24 | protected $userAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'; |
||
25 | /** |
||
26 | * @var HttpInterface |
||
27 | */ |
||
28 | protected $http; |
||
29 | protected $loggedIn; |
||
30 | protected $cookieJar; |
||
31 | protected $options; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $filePathToUpload; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $csrfToken = ''; |
||
43 | |||
44 | /** |
||
45 | * Common headers needed for every query. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $requestHeaders = [ |
||
50 | 'Accept: application/json, text/javascript, */*; q=0.01', |
||
51 | 'Accept-Language: en-US,en;q=0.5', |
||
52 | 'DNT: 1', |
||
53 | 'Host: nl.pinterest.com', |
||
54 | 'X-Pinterest-AppState: active', |
||
55 | 'X-NEW-APP: 1', |
||
56 | 'X-APP-VERSION: 04cf8cc', |
||
57 | 'X-Requested-With: XMLHttpRequest', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $postFileData; |
||
64 | |||
65 | /** |
||
66 | * @param HttpInterface $http |
||
67 | */ |
||
68 | public function __construct(HttpInterface $http) |
||
69 | { |
||
70 | $this->http = $http; |
||
71 | $this->loggedIn = false; |
||
72 | $this->cookieJar = tempnam(sys_get_temp_dir(), self::COOKIE_NAME); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Executes api call for follow or unfollow user. |
||
77 | * |
||
78 | * @param int $entityId |
||
79 | * @param string $entityName |
||
80 | * @param string $url |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function followMethodCall($entityId, $entityName, $url) |
||
102 | |||
103 | /** |
||
104 | * @param string $pathToFile |
||
105 | * @param string $url |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function upload($pathToFile, $url) |
||
115 | |||
116 | /** |
||
117 | * Executes request to Pinterest API. |
||
118 | * |
||
119 | * @param string $resourceUrl |
||
120 | * @param string $postString |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | public function exec($resourceUrl, $postString = '') |
||
133 | |||
134 | /** |
||
135 | * Adds necessary curl options for query. |
||
136 | * |
||
137 | * @param string $postString POST query string |
||
138 | * |
||
139 | * @return $this |
||
140 | */ |
||
141 | protected function makeHttpOptions($postString = '') |
||
156 | |||
157 | /** |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function setDefaultHttpOptions() |
||
174 | |||
175 | /** |
||
176 | * @param array $options |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | protected function addDefaultCsrfInfo($options) |
||
187 | |||
188 | /** |
||
189 | * Clear token information. |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function clearToken() |
||
199 | |||
200 | /** |
||
201 | * Mark api as logged. |
||
202 | * @return $this |
||
203 | * @throws AuthException |
||
204 | */ |
||
205 | public function login() |
||
210 | |||
211 | public function logout() |
||
216 | |||
217 | /** |
||
218 | * Get log status. |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isLoggedIn() |
||
226 | |||
227 | /** |
||
228 | * @param $userAgent |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function setUserAgent($userAgent) |
||
239 | |||
240 | /** |
||
241 | * Create request string. |
||
242 | * |
||
243 | * @param array $data |
||
244 | * @param array $bookmarks |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public static function createQuery(array $data = [], $bookmarks = []) |
||
255 | |||
256 | /** |
||
257 | * @param array|object $data |
||
258 | * @param array $bookmarks |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | public static function createRequestData(array $data = [], $bookmarks = []) |
||
279 | |||
280 | public function setTokenFromCookies() |
||
289 | |||
290 | /** |
||
291 | * @return array |
||
292 | */ |
||
293 | protected function getDefaultHttpHeaders() |
||
299 | |||
300 | /** |
||
301 | * If we are uploading file, we should build boundary form data. Otherwise |
||
302 | * it is simple urlencoded form. |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | protected function getContentTypeHeader() |
||
320 | |||
321 | /** |
||
322 | * @param string $delimiter |
||
323 | * @return $this |
||
324 | */ |
||
325 | protected function buildFilePostData($delimiter) |
||
337 | } |
||
338 |