'Implementing Rawg Api wordpress not working

I try to implement the Rawg.io API to my Wordpress Functions.php File but it doesn't work, i tried my code with another API and it is working fine, i think it has something to do with the API link Page='.$current_page.'

as you can see i created an custom post type to add the games to. i also created custom fields with the right field keys.

My Report.txt file keeps returning current page = 1

function register_game_cpt() {
  register_post_type( 'game', array(
    'label' => 'Games',
    'public' => true,
    'capability_type' => 'post',
    'supports' => array('title', 'editor', 'thumbnail'),
    'taxonomies' => array('recordings', 'category', 'whatever', 'post_tag'),
  ));
}
add_action( 'init', 'register_game_cpt' );

// if ( ! wp_next_scheduled( 'update_game_list' ) ) {
//   wp_schedule_event( time(), 'weekly', 'update_game_list' );
// }
add_action( 'update_game_list', 'get_games_from_api' );
add_action( 'wp_ajax_nopriv_get_games_from_api', 'get_games_from_api' );
add_action( 'wp_ajax_get_games_from_api', 'get_games_from_api' );
function get_games_from_api() {

  $file = get_stylesheet_directory() . '/report.txt';
  $current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
  $games = [];

  // Should return an array of objects
  $results = wp_remote_retrieve_body(wp_remote_get('https://api.rawg.io/api/games?key=/////////////////////&page='.$current_page.'&page_size=40'));
  file_put_contents($file, "Current Page: " . $current_page. "\n\n", FILE_APPEND);

  // turn it into a PHP array from JSON string
  $results = json_decode( $results );   
  
  // Either the API is down or something else spooky happened. Just be done.
  if( ! is_array( $results ) || empty( $results ) ){
    return false;
  }

  $games[] = $results;

  foreach( $games[0] as $game ){
    $game_slug = sanitize_title( $game->name . '-' . $game->id );     
    $existing_game = get_page_by_path( $game_slug, 'OBJECT', 'game' );

    if( $existing_game === null  ){
      $inserted_game = wp_insert_post( [
        'post_name' => $game_slug,
        'post_title' => $game_slug,
        'post_type' => 'game',
        'post_status' => 'publish'
      ] );
      if( is_wp_error( $inserted_game ) || $inserted_game === 0 ) {
        die('Could not insert game: ' . $game_slug);
        error_log( 'Could not insert game: ' . $game_slug );
        continue;
      }
      // add meta fields
      $fillable = [
        'field_62684fc72d524' => 'count',
        'field_6266cb41982d3' => 'name',
        'field_6266cb4c982d4' => 'publishers',
        'field_6266cb54982d5' => 'genres',
        'field_6266cb64012e9' => 'platforms',
        'field_6266cb722ebe8' => 'dates',
        'field_626850012d525' => 'results',
      ];
      foreach( $fillable as $key => $name ) {
        update_field( $key, $game->$name, $inserted_game );
      }
    } else {
      $existing_game_id = $existing_game->ID;
      $exisiting_game_timestamp = get_field('updated_at', $existing_game_id);

      if( $game->updated_at >= $exisiting_game_timestamp ){
        $fillable = [
          'field_62684fc72d524' => 'count',
          'field_6266cb41982d3' => 'name',
          'field_6266cb4c982d4' => 'publishers',
          'field_6266cb54982d5' => 'genres',
          'field_6266cb64012e9' => 'platforms',
          'field_6266cb722ebe8' => 'dates',
          'field_626850012d525' => 'results',
        ];
        foreach( $fillable as $key => $name ){
          update_field( $name, $game->$name, $existing_game_id);
        }
      }
    }
  }
  $current_page = $current_page + 1;
  wp_remote_post( admin_url('admin-ajax.php?action=get_games_from_api'), [
    'blocking' => false,
    'sslverify' => false, // we are sending this to ourselves, so trust it.
    'body' => [
      'current_page' => $current_page
    ]
  ] );
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source