public static function getMediaCommentsByCode($code, $count = 10, $maxId = null) { $remain = $count; $comments = []; $index = 0; $hasPrevious = true; while ($hasPrevious && $index < $count) { if ($remain > self::MAX_COMMENTS_PER_REQUEST) { $numberOfCommentsToRetreive = self::MAX_COMMENTS_PER_REQUEST; $remain -= self::MAX_COMMENTS_PER_REQUEST; $index += self::MAX_COMMENTS_PER_REQUEST; } else { $numberOfCommentsToRetreive = $remain; $index += $remain; $remain = 0; } if (!isset($maxId)) { // $response = Request::get(Endpoints::getLastCommentsByCodeLink($code, $numberOfCommentsToRetreive)); $parameters = Endpoints::getLastCommentsByCodeLink($code, $numberOfCommentsToRetreive); } else { // $response = Request::get(Endpoints::getCommentsBeforeCommentIdByCode($code, $numberOfCommentsToRetreive, $maxId)); $parameters = Endpoints::getCommentsBeforeCommentIdByCode($code, $numberOfCommentsToRetreive, $maxId); } // if ($response->code === 404) { // throw new InstagramNotFoundException('Account with given username does not exist.'); // } // if ($response->code !== 200) { // throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.'); // } $jsonResponse = json_decode(self::getContentsFromUrl($parameters), true); $nodes = $jsonResponse['comments']['nodes']; foreach ($nodes as $commentArray) { $comments[] = Comment::fromApi($commentArray); } $hasPrevious = $jsonResponse['comments']['page_info']['has_previous_page']; $numberOfComments = $jsonResponse['comments']['count']; if ($count > $numberOfComments) { $count = $numberOfComments; } if (sizeof($nodes) == 0) { return $comments; } $maxId = $nodes[sizeof($nodes) - 1]['id']; } return $comments; }