diff --git a/README.md b/README.md index 9918b79c..68622612 100644 --- a/README.md +++ b/README.md @@ -198,5 +198,18 @@ $medias = Instagram::getLocationTopMediasById(1); $medias = Instagram::getLocationMediasById(1); ``` +### Get followers of an account +```php +$username = 'kevin'; +$followers = []; +$instagram = Instagram::withCredentials('your_username', 'hunter2'); +$instagram->login(); +sleep(2); // Delay to mimic browser +$account = Instagram::getAccount($username); +sleep(1); +$followers = $instagram->getFollowers($account->getId(), 1000, 100, true); // Get 1000 followers of 'kevin', 100 a time with random delay between requests +echo '
' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ''; +``` + ### Other Java library: https://github.com/postaddictme/instagram-java-scraper \ No newline at end of file diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index c14f1476..181ff04d 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -734,7 +734,7 @@ public function getFollowers($accountId, $count = 20, $pageSize = 20, $delayed = throw new InstagramException('Count must be greater than or equal to page size.'); } - while ($index < $count) { + while (true) { $response = Request::get(Endpoints::getFollowersJsonLink($accountId, $pageSize, $endCursor), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { @@ -754,7 +754,10 @@ public function getFollowers($accountId, $count = 20, $pageSize = 20, $delayed = foreach ($edgesArray as $edge) { $accounts[] = $edge['node']; - $index++; + $index++; + if ($index >= $count) { + break 2; + } } $pageInfo = $jsonResponse['data']['user']['edge_followed_by']['page_info'];