public function next() { $this->itemCursor++; // if cursor points at result of next page, try to load it if ($this->itemCursor < $this->perPage || $this->itemCursor % $this->perPage !== 0) { return; } if (isset($this->resources[$this->pageCursor + 1])) { // already loaded $this->itemCursor = 0; $this->pageCursor++; return; } if ($this->loadedMaxResults()) { return; } try { $prevRequest = $this->responses[$this->pageCursor]->getRequest(); // Get all request parameters $params = $this->responses[$this->pageCursor]->request->getParameters(); // Get last record $current = Nette\Utils\ArrayHash::from($this->resources[$this->pageCursor][$this->itemCursor - 1]); // And set maximum ID $params['max_id'] = $current->id; // Get requested path $path = $prevRequest->getUrl()->getPath(); $path = ltrim($path, '/1.1/'); $response = $this->httpClient->makeRequest($prevRequest->copyWithUrl($this->client->getConfig()->createUrl('api', $path, $params)), 'HMAC-SHA1'); $this->itemCursor = 0; $this->pageCursor++; $this->responses[$this->pageCursor] = $response; $this->resources[$this->pageCursor] = $response->toArray(); } catch (\Exception $e) { $this->itemCursor--; // revert back so the user can continue if needed } }
/** * Upload photo to the Twitter * * @param string $file * @param string|null $status * * @return Utils\ArrayHash * * @throws Exceptions\FileNotFoundException * @throws Exceptions\InvalidArgumentException * @throws OAuth\Exceptions\ApiException|static */ public function uploadMedia($file, $status = NULL) { if (!is_file($file)) { throw new Exceptions\FileNotFoundException("File '{$file}' does not exists. Please provide valid path to file."); } if ($status !== NULL && !is_string($status)) { throw new Exceptions\InvalidArgumentException("Status text '{$status}' have to be a string. Please provide valid status text."); } if ($status !== NULL) { $post = ['media[]' => new \CURLFile($file), 'status' => $status]; $result = $this->post('statuses/update_with_media.json', [], $post); return $result instanceof Utils\ArrayHash ? $result : new Utils\ArrayHash(); } else { // Add file to post params $post = ['media' => new \CURLFile($file)]; $response = $this->httpClient->makeRequest(new Api\Request($this->consumer, $this->config->createUrl('upload', 'media/upload.json'), Api\Request::POST, $post, [], $this->getAccessToken()), 'HMAC-SHA1'); if ($response->isOk() && $response->isJson() && ($data = Utils\ArrayHash::from($response->toArray()))) { return $data; } else { $ex = $response->toException(); throw $ex; } } }