user2668414
user2668414

Reputation: 41

Looping through users with Instagram API requests is REALLY slow

I'm trying to loop through a list of user IDs to check the relationship status on a user. This (limiting the loop to 20) takes long enough to get the "fatal PHP couldn't process in 30 secs error". ...and I would LIKE to do quite a bit more than 20 repetitions.

Is there a way to make "batch" requests? Sending IG a list of ALL user IDs I want to check in one swoop?

Here's my current snippet:

<?php 
$i = 0;
foreach(get_user_meta($user_id, 'followed_users', false) as $followed){
        if($i < 20){
                        //Makes sure it's a real Insta user ID
            if(strlen($followed) > 2){
            $relationshipInfo = $instagram->getUserRelationship($followed);
            $relationship = $relationshipInfo->data->outgoing_status;
                if( $relationship == 'none' ){
                    //BAN THEM
                    update_user_meta($user_id, 'is_banned', 1);
                    if(!is_page('banned') && (get_user_meta($user_id, 'is_banned', true) == 1)){    
                        //REDIRECT TO 'BANNED' PAGE             
                        $redirect = get_bloginfo("url").'/banned/';
                        wp_redirect( $redirect );
                        exit;
                    }
                } else {
                    //DON'T BAN
                    update_user_meta($user_id, 'is_banned', 0);
                }
            }
        }
        $i++;
    }
?>

I do just want to note: I know I could store a list of all users $current_user follows and check with foreach and inarray to find out if $current_user unfollowed anyone (as the list of users $current_user followed through my site is stored to the database) except I'm having trouble getting the FULL list of users $current_user follows... so solving that would also, in effect, solve my dilemma.

EDIT: Although I still would like to know about a faster looping method, somehow, getting (almost) an accurate number of users $current_user is following seems to have started working... (???)...

For anyone else trying to do something along the same lines as I, perhaps this code can point you in the right direction until someone answers the original.

$insta_id = get_user_meta($current_user->ID, 'instagram_id', true);
$fObject = $instagram->getUserFollows($insta_id, -1);
$fUsers = $fObject->data;
//generate array of users currently followed
$fArray = array();
foreach ($fUsers as $fUser) {
    $fArray[] = $fUser->id;
}
//list of people followed through insta-hashtag
$wasFolloweds = get_user_meta($current_user->ID, 'followed_users', false);
foreach ($wasFolloweds as $wasF) {
    //check if they unfollowed
    if(strlen($wasF) > 2){
        if(!in_array($wasF, $fArray)){
            $userinfo = $instagram->getUserRelationship($wasF);
            if(!$userinfo->meta->error_type == 'APINotAllowedError'){ 
                //BAN THEM
                update_user_meta($user_id, 'is_banned', 1);
                if(!is_page('banned') && (get_user_meta($current_user->ID, 'is_banned', true) == 1)){   
                    //REDIRECT TO 'BANNED' PAGE             
                    $redirect = get_bloginfo("url").'/banned/';
                    wp_redirect( $redirect );
                    exit;
                }  
            } else {
                //DON'T BAN
                update_user_meta($user_id, 'is_banned', 0);
            }
        } else {
                //DON'T BAN
                update_user_meta($user_id, 'is_banned', 0);
        }
    }
}

Upvotes: 3

Views: 1135

Answers (1)

rsnickell
rsnickell

Reputation: 1262

Doing the requests sequentially will take longer than doing the requests in parallel. You'll want to do concurrent requests to speed this up.

Take a look at this answer using curl_multi.

Upvotes: 3

Related Questions