enisz / igdb

IGDB PHP API Wrapper
GNU General Public License v3.0
27 stars 8 forks source link

"cover" is an integer instead of an object #13

Closed RazenIW closed 3 years ago

RazenIW commented 3 years ago

Hello,

Considering the following options :

$options` = array(
        'search' => 'uncharted 4',
        'fields' => array(
            'id',
            'name',
            'cover'
        )
);

I get these results :

array(5) {
  [0]=>
  object(stdClass)#2 (3) {
    ["id"]=> int(7331)
    ["cover"]=> int(81917)
    ["name"]=> string(26) "Uncharted 4: A Thief's End"
  }
  [1]=>
  object(stdClass)#3 (2) {
    ["id"]=> int(136686)
    ["name"]=> string(43) "Uncharted 4: A Thief's End - Deluxe Edition"
  }
  [2]=>
  object(stdClass)#4 (3) {
    ["id"]=> int(41874)
    ["cover"]=> int(100142)
    ["name"]=> string(42) "Uncharted 4: A Thief's End Special Edition"
  }
  [3]=>
  object(stdClass)#5 (3) {
    ["id"]=> int(41879)
    ["cover"]=> int(110424)
    ["name"]=> string(57) "Uncharted 4: A Thief's End Libertalia Collector's Edition"
  }
  [4]=>
  object(stdClass)#6 (3) {
    ["id"]=> int(112090)
    ["cover"]=> int(69356)
    ["name"]=> string(18) "Uncharted Water IV"
  }
}

The field "cover" is an integer instead of an object like described in your documentation, am I doing anything wrong ?

Here's the full code :

require 'class.igdb.php';

$IGDB = new IGDB("<client_id>", "<token>");

$options = array(
    'search' => 'uncharted 4',
    'fields' => array(
        'id',
        'name',
        'cover'
    )
);

try {
    $result = $IGDB->game($options);

    echo "<pre>";
    var_dump($result);
    echo "</pre>";
}

catch (Exception $e) {
    echo $e->getMessage();
}

Thanks!

enisz commented 3 years ago

Hi @RazenIW,

Thank you for using my wrapper! I just checked the examples I made eariler and I cannot find any with the cover field being an object. Can you tell me where did you see this? Or where this example is located? It is possible that it worked different on earlier versions of IGDB API and I forgot to update the documentation somewhere.

Currently the API will return a cover id in this field. If you check the game endpoint documentation you can see that the cover field is a reference id for a cover object. In this case you can either:

I recommend using the expander. This way you can get every information you want in a single query. To use the expander update your options array as below:

$options = array(
    'search' => 'uncharted 4',
    'fields' => array(
        'id',
        'name',
        'cover.*' // note the expander feature here
    )
);

Using these options the return value of the query changes as below:

array (size=5)
  0 => 
    object(stdClass)[2]
      public 'id' => int 7331
      public 'cover' => 
        object(stdClass)[3]
          public 'id' => int 81917
          public 'alpha_channel' => boolean false
          public 'animated' => boolean false
          public 'game' => int 7331
          public 'height' => int 960
          public 'image_id' => string 'co1r7h' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co1r7h.jpg' (length=54)
          public 'width' => int 720
          public 'checksum' => string 'd2893576-b1fe-06e2-c813-2896e1bba9a1' (length=36)
      public 'name' => string 'Uncharted 4: A Thief's End' (length=26)
  1 => 
    object(stdClass)[4]
      public 'id' => int 136686
      public 'name' => string 'Uncharted 4: A Thief's End - Deluxe Edition' (length=43)
  2 => 
    object(stdClass)[5]
      public 'id' => int 41874
      public 'cover' => 
        object(stdClass)[6]
          public 'id' => int 100142
          public 'alpha_channel' => boolean false
          public 'animated' => boolean false
          public 'game' => int 41874
          public 'height' => int 993
          public 'image_id' => string 'co259q' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co259q.jpg' (length=54)
          public 'width' => int 744
          public 'checksum' => string '7f899c70-4c8a-fc64-0901-74d01d32638a' (length=36)
      public 'name' => string 'Uncharted 4: A Thief's End Special Edition' (length=42)
  3 => 
    object(stdClass)[7]
      public 'id' => int 41879
      public 'cover' => 
        object(stdClass)[8]
          public 'id' => int 110424
          public 'alpha_channel' => boolean false
          public 'animated' => boolean false
          public 'game' => int 41879
          public 'height' => int 1687
          public 'image_id' => string 'co2d7c' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co2d7c.jpg' (length=54)
          public 'width' => int 1265
          public 'checksum' => string 'bebab7e4-a94d-7ff5-3fc3-ea16e1a3ed2f' (length=36)
      public 'name' => string 'Uncharted 4: A Thief's End Libertalia Collector's Edition' (length=57)
  4 => 
    object(stdClass)[9]
      public 'id' => int 112090
      public 'cover' => 
        object(stdClass)[10]
          public 'id' => int 69356
          public 'alpha_channel' => boolean false
          public 'animated' => boolean false
          public 'game' => int 112090
          public 'height' => int 444
          public 'image_id' => string 'co1hik' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co1hik.jpg' (length=54)
          public 'width' => int 492
          public 'checksum' => string '15be2383-507c-becd-a6ab-d02ecccfa3f1' (length=36)
      public 'name' => string 'Uncharted Water IV' (length=18)

The cover object in the results will contain all fields of the cover endpoint. Of course, you can fine tune this by using specific fields to exclude unnecessary information.

$options = array(
    'search' => 'uncharted 4',
    'fields' => array(
        'id',
        'name',
        'cover.image_id', // view the image_id
        'cover.url'       // and url fields only
    )
);

The result will be:

array (size=5)
  0 => 
    object(stdClass)[2]
      public 'id' => int 7331
      public 'cover' => 
        object(stdClass)[3]
          public 'id' => int 81917
          public 'image_id' => string 'co1r7h' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co1r7h.jpg' (length=54)
      public 'name' => string 'Uncharted 4: A Thief's End' (length=26)
  1 => 
    object(stdClass)[4]
      public 'id' => int 136686
      public 'name' => string 'Uncharted 4: A Thief's End - Deluxe Edition' (length=43)
  2 => 
    object(stdClass)[5]
      public 'id' => int 41874
      public 'cover' => 
        object(stdClass)[6]
          public 'id' => int 100142
          public 'image_id' => string 'co259q' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co259q.jpg' (length=54)
      public 'name' => string 'Uncharted 4: A Thief's End Special Edition' (length=42)
  3 => 
    object(stdClass)[7]
      public 'id' => int 41879
      public 'cover' => 
        object(stdClass)[8]
          public 'id' => int 110424
          public 'image_id' => string 'co2d7c' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co2d7c.jpg' (length=54)
      public 'name' => string 'Uncharted 4: A Thief's End Libertalia Collector's Edition' (length=57)
  4 => 
    object(stdClass)[9]
      public 'id' => int 112090
      public 'cover' => 
        object(stdClass)[10]
          public 'id' => int 69356
          public 'image_id' => string 'co1hik' (length=6)
          public 'url' => string '//images.igdb.com/igdb/image/upload/t_thumb/co1hik.jpg' (length=54)
      public 'name' => string 'Uncharted Water IV' (length=18)

Hope this helps!

RazenIW commented 3 years ago

Hello,

I tried using the expander feature and it works now 👍 This is where I saw cover fields being objects instead of integers at first.

Thanks a lot !

enisz commented 3 years ago

Hi,

Sorry, I saw your last comment just forgot to answer it. The issue you linked was related of an earlier version of the IGDB api where the cover was an object. Since then it got changed.

Thanks for closing the issue!