user8964341
user8964341

Reputation:

mgp25 Instagram-API too many API requests

I want to get all followers of two entered users. I did this but when tried the users have a million followers, for example (5 Million, etc...), I got this error from Instagram.

In my example:

-> User1 has 5 Million followers

-> User2 has 7 Millon followers

I need to get these user's followers then store two array and compare each other.

I did this but only small accounts...

Something went wrong: Throttled by Instagram because of too many API requests. Something went wrong: Throttled by Instagram because of too many API requests.

Here my code:

    require 'vendor/autoload.php';

\InstagramAPI\Instagram::$allowDangerousWebUsageAtMyOwnRisk = true;

$username = 'test_35_1041';
$password = '*****';

$ig = new \InstagramAPI\Instagram();

try {
    $ig->login($username,$password);
} catch (\Exception $e) {
    echo $e->getMessage();
}
if (!empty($_POST)){

    $name1 = $_POST["username1"];
    $name2 = $_POST["username2"];

    $userId = $ig->people->getUserIdForName($name1);
    $userId2 = $ig->people->getUserIdForName($name2);

    $items = array();
    $items2 = array();

    $time_start = microtime(true);
   try {
        $maxId = null;
        $rankToken = \InstagramAPI\Signatures::generateUUID();
        do {
            $response = $ig->people->getFollowers($userId,$rankToken,null,$maxId);
            $json = json_decode($response);
            foreach ($json->users as $value) {
                # code...
                $items[] = $value->username;
                //print_r($value->username);
            }
            $maxId = $response->getNextMaxId();
            flush();
        } while ($maxId !== null);
    } catch (\Exception $e) {
        echo 'Something went wrong: '.$e->getMessage()."\n";
    }

    try {
        $maxId = null;
        $rankToken = \InstagramAPI\Signatures::generateUUID();
        do {
            $response = $ig->people->getFollowers($userId2,$rankToken,null,$maxId);
            $json = json_decode($response);
            foreach ($json->users as $value) {
                # code...
                $items2[] = $value->username;
                //print_r($value->username);
            }
            $maxId = $response->getNextMaxId();
        } while ($maxId !== null);
    } catch (\Exception $e) {
        echo 'Something went wrong: '.$e->getMessage()."\n";
    }
    $time_end = microtime(true);
    $execution_time = ($time_end - $time_start)/60;

    $result = array_intersect($items, $items2);
}

I think its more complex, ı need to optimize this code for your help pls... How can ı solve this problem?

Thanks Everyone.

Upvotes: 2

Views: 4602

Answers (1)

Nabijon Juraev
Nabijon Juraev

Reputation: 66

Throttled by Instagram because of too many API requests

It means you send requests to often. You need to sleep on every loop step like this

try {
    $maxId = null;
    $rankToken = \InstagramAPI\Signatures::generateUUID();
    do {
        ****
        Log::info( "Sleeping for 5s...");
        sleep(5);
    } while ($maxId !== null);
} catch (\Exception $e) {
    echo 'Something went wrong: '.$e->getMessage()."\n";
}

Upvotes: 0

Related Questions