php - Looping through users with Instagram API requests is REALLY slow -
i'm trying loop through list of user ids check relationship status on user. (limiting loop 20) takes long enough "fatal php couldn't process in 30 secs error". ...and quite bit more 20 repetitions.
is there way make "batch" requests? sending ig list of user ids want check in 1 swoop?
here's current snippet:
<?php $i = 0; foreach(get_user_meta($user_id, 'followed_users', false) $followed){ if($i < 20){ //makes sure it's 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 '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 want note: know store list of users $current_user follows , check foreach , inarray find out if $current_user unfollowed (as list of users $current_user followed through site stored database) except i'm having trouble getting full list of users $current_user follows... solving also, in effect, solve dilemma.
edit: although still know faster looping method, somehow, getting (almost) accurate number of users $current_user following seems have started working... (???)...
for else trying along same lines i, perhaps code can point in right direction until answers 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 followed $farray = array(); foreach ($fusers $fuser) { $farray[] = $fuser->id; } //list of people followed through insta-hashtag $wasfolloweds = get_user_meta($current_user->id, 'followed_users', false); foreach ($wasfolloweds $wasf) { //check if 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 '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); } } }
doing requests sequentially take longer doing requests in parallel. you'll want concurrent requests speed up.
take @ this answer using curl_multi.
Comments
Post a Comment