• I have a function that fetches data from an API and then creates a user for each ID that it finds. For some reason once the function processes the webpage gets hungup for a long time and the pages just keeps loading, and loading. But if I go to the list of profiles everything imported fine. So I thought it would have to be an infinite loop of some kind, but when I ran the code in an IDE and there was no infinite loop. Any ideas?

    Code below if needed:

    function af_askFRED_search_club() {
        $af_club_id = af_askFRED_get_club_id();
        $af_per_page = 100;
        $page = 1;
        $af_get_url = "https://api.askfred.net/v1/fencer/?_api_key=abunchofnumbers&club_id=".$af_club_id."&_per_page=".$af_per_page."&_page=";
        $af_get_request = $af_get_url.$page;
        $af_get_response = file_get_contents($af_get_request);
        $af_decoded = json_decode($af_get_response, true);
        $af_count = floor($af_decoded['total_matched'] / $af_per_page) + 1;
    
        for ($i = 1; $i = $af_count; $i++) {
    
            if ($i != 1) {
                $af_get_request = $af_get_url.$page;
                $af_get_response = file_get_contents($af_get_request);
                $af_decoded = json_decode($af_get_response, true);
            }
    
            $af_decoded_fencers = ($af_decoded['fencers']);
            $af_decoded_fencers_length = count($af_decoded_fencers);
    
            for ($p = 0; $p <= $af_decoded_fencers_length; $p++) {
    
                $fencer_array = $af_decoded_fencers[$p];
    
                if (!empty($fencer_array['usfa_id'])) {
                    $fencer_array_decoded = array(
                        'first_name' => $fencer_array['first_name'],
                        'last_name' => $fencer_array['last_name'],
                        'usfa_id' => $fencer_array['usfa_id']
                    );
    
                    $username = $fencer_array_decoded['first_name']." ".$fencer_array_decoded['last_name'];
                    $password = $fencer_array_decoded['usfa_id'];
    
                    while (username_exists($username) != null){
                        $i++;
                        $username = $username.$i;
                    }
    
                    wp_insert_user(array(
                        'user_login' => $username,
                        'user_pass'  => $password,
                        'role'       => 'fencer'
    
                    ));
    
                    $user = get_user_by('login', $username);
                    $userid = $user->ID;
                    update_user_meta($userid, 'af_askFRED_usfaID', $password);
    
                }
            }
    
            $page++;
        }
    
    }

  • The topic ‘Infinite Loop that isn't an infinite loop’ is closed to new replies.