PHP Instagram Scraping Load More button Issue

I'm doing a script to scrape Instagram posts from a username however I'm having issues with the load more function and how to get the next 12 posts.

Here is my function so far:

function scrape_insta_user_images($username){
    $insta_source = file_get_contents('https://www.instagram.com/'.$username.'/'); // instagram user url
    $shards = explode('window._sharedData = ', $insta_source);
    $insta_json = explode(';</script>', $shards[1]); 
    $insta_array = json_decode($insta_json[0], TRUE);
    $results_array = $insta_array;
    $image_array= array(); // array to store images.
    $json = array();
    $json['profile_pic']    = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url'];
    $json['has_next_page']  = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page'];
    $json['max_id']         = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'];

    $limit = 13;

    for ($i=0; $i < $limit; $i++) {     
                if(isset($insta_array['media'][$i])){
                    $latest_array = $insta_array['media'][$i];
                    $json['images'][]  = $latest_array['code'];
                }
    }

    $limit = 56; 
    for ($i=0; $i < $limit; $i++) {     
        if(isset($results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i])){
            $latest_array = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node'];
            $json['images'][] = $latest_array['shortcode']; // thumbnail and same sizes 
        }
    }
    return $json;
}

It also returns bunch of tokens or end_cursor values. However I can't figure it out what parameters to send to get the next 12 posts. Instagram does a request to such URL:

https://www.instagram.com/graphql/query/?query_hash=XXX&variables={"id":"XXX","first":12,"after":"XXX"}

Any help would be much appareciated!