mgp25 Instagram-API的API请求过多

问题描述 投票:0回答:1

我想获得两个输入用户的所有关注者。我是这样做的,但是当用户尝试使用一百万个粉丝时(例如500万,等等),我从Instagram收到此错误。

在我的示例中:

-> User1有500万关注者

-> User2有7个Millon关注者

我需要获得这些用户的关注者,然后存储两个数组并相互比较。

我做了这个,但只有小数目...

出了点问题:由于API请求过多,Instagram被节制。出了点问题:由于API请求过多,Instagram受节制。

这里是我的代码:

    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);
}

我认为它更复杂,我需要为您的帮助优化此代码...我该如何解决这个问题?

谢谢大家。

php composer-php instagram-api
1个回答
0
投票

由于过多的API请求而被Instagram限制

这意味着您经常发送请求。您需要在每个循环步骤中都这样睡觉]

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";
}
© www.soinside.com 2019 - 2024. All rights reserved.