Avatar image for cook66
#1 Posted by Cook66 (272 posts) -

Hey. I've been working on a private project for a while now and thought I'd try to integrate some of the data available on GiantBomb, but I have no experience working with xml and only some with PHP. I want to start by making a simple search tool which uses the search API. I want to create a simple list of the results with PHP just to learn how to do this.

Can any of you point me in the right direction?

Avatar image for zerok
#2 Posted by zerok (216 posts) -

The SimpleXML API introduced with PHP5 makes working with XML actually pretty easy, but you won't get around reading at least some manuals ;-)


This example should give you at least some idea how to use the search API.
Avatar image for cook66
#3 Posted by Cook66 (272 posts) -

Thanks. SimpleXML is working great. I've read a few manuals now and I've got a basic search working already. :)

Avatar image for notso
#4 Edited by Notso (24 posts) -

@zerok   Thanks a ton. Your small, php program is well written and helps me a ton on figuring out how to use the GB API.

Avatar image for notso
#5 Posted by Notso (24 posts) -

Hello anyone, 

 I am fairly new at this. I am using a variation on zerok's code, and I now want to grab an image and display it.  However, I can't get it to work. I think I may need to use  simplexml_load_file() instead of  simplexml_load_string() but using the ...file() method just breaks the name and site_detail_url and doesn't display the image.  Below is the code as I have it now (the code that doesn't work). Is anyone able to give me a hint as to how to parse and display an image from the API in PHP?

  

 <?php

define('API_KEY' , 'myKey' );

$base_url = "http://api.giantbomb.com/characters/" ;


  $fields = array();

    $fields['field_list' ] = 'name,image,site_detail_url' ;

    $fields[ 'limit'] = '10';

    $fields[ 'offset'] = '165';

    $fields[ 'api_key'] = API_KEY;

    $url = $base_url . '?' . http_build_query($fields);

    $data = file_get_contents($url);

    $xml = simplexml_load_string($data);


if ( isset($xml)): ?>

<html >

<head >< title >Video Game Characters </ title ></head >

<body >

< h2 >Results: </ h2 >

<ul >

    <?php foreach ($xml-> results->children() as $item): ?>

    < li >

        < a href=" <?php echo $item-> site_detail_url ?>" > <?php echo $item-> name ?> </ a >

        < br />

        < img src=" <?php echo $item-> image ?>" >

    </ li >

    <?php endforeach ; ?>

</ ul>

</body >

</html >

<?php endif ?>

Avatar image for lordandrew
#6 Posted by LordAndrew (14601 posts) -

In what way does it not work? What is your output? Any errors?

Avatar image for notso
#7 Edited by Notso (24 posts) -

all I get is the little broken img  images. I get the name with the link and below that the default broken img pic. No errors or warnings in my Eclipse IDE.

Avatar image for lordandrew
#8 Posted by LordAndrew (14601 posts) -

Now we're getting somewhere. What is in the src attribute set to for those img tags?

Avatar image for notso
#9 Posted by Notso (24 posts) -

I have the $item->image in there because I have added image in the field_list to return. I am not sure what image returns. the API doc just says    Main Image of the character

Avatar image for lordandrew
#10 Posted by LordAndrew (14601 posts) -

I see that in your source code. But what is the actual value that ends up in the HTML?

Avatar image for notso
#11 Edited by Notso (24 posts) -

trying to figure out the Eclipse PHP Debugger now to get that . Well I can't figure the debugger out, but if I do a View Source on the html page the list item looks like this 
  
 </li>          <li> 
        <a href="http://www.giantbomb.com/master-chief/94-175/">Master Chief</a> 
        <br /> 
                <br /> 
                <br /> 
        <img src=" " > 
    </li>

Avatar image for lordandrew
#12 Posted by LordAndrew (14601 posts) -

Okay, so it's blank. I think I understand why. I'll copy this to my localhost installation see what I can do.

Avatar image for lordandrew
#13 Posted by LordAndrew (14601 posts) -
@Notso: Giant Bomb has mangled the HTML and it won't render. Please paste your source code to a site like Pastebin and provide me with the link, that will make things easier for both of us.
Avatar image for notso
#14 Posted by Notso (24 posts) -
Avatar image for lordandrew
#15 Edited by LordAndrew (14601 posts) -

Never mind. I think I got it. Instead of "$item->image", use "$item->image->super_url". Note that they're nested. Of course, you can use whatever spacing style you wish, I just wasn't sure which was intended.

Avatar image for notso
#16 Edited by Notso (24 posts) -

K. I'll give it a shot.   
 
Okay, I had a note here that it didn't work, but it did. I had to make one little adjustment, and it is working now. Thanks, LordAndrew!
 
BTW, as an aside, I am not getting anything when I add enemies to my field_list and then to my php to display it. I'll tackle that another time, but I am curious if the issue is similar to that with the images. 
 
Well, I need to go for now,  and it seems I have reached my post limit for flood protection.

Avatar image for lordandrew
#17 Posted by LordAndrew (14601 posts) -
$fields['field_list'] = 'name,deck,enemies,site_detail_url,image';
You have to add "image" to your field_list, like I've done above. Replace line 6 with the provided line. The reason it wasn't appearing in the output was because it wasn't included in the API results. Personally, I'm not using the field_list filter in my current work. But that's mostly because I'm dumping all the data and therefore will need all the fields.
 
Each enemy will be an $item->enemies->character, and from each of those you'd probably want to use its name and site_detail_url. But we can discuss this more tomorrow if you still need help.
Avatar image for darg
#18 Posted by Darg (34 posts) -

Just for your information: using the JSON delivery format and json_decode() is going to be faster than using SimpleXML.

Avatar image for notso
#19 Posted by Notso (24 posts) -

Thanks, dargonaut. I was just learning at the time (and still am) so I stuck with XML while just figuring this stuff out.


@LordAndrew - I know this is late, but I just wanted to thank you again for all your help. What I ended up doing was grabbing 10 characters and then, using the id numbers I got, I did a search for the games those characters were in. I then displayed a page that showed those 10 characters (with a pic and some details from the characters resource) along with the games they were in (from the character resource). It worked well for learning how to take in some API data, work with it a little, and build a simple page with it (which was my goal). Thanks again.
Avatar image for modestink
#20 Edited by ModestINK (2 posts) -

@LordAndrew: instead of super_url is there a smaller image url?

EDIT:resolved my issue -- instead of super_url you can place small_url or thumb_url ! @LordAndrew thanks for your above posts helped me out with my api setup !

Avatar image for lordandrew
#21 Posted by LordAndrew (14601 posts) -
@ModestINK: Ah, glad to see you got that worked out. :)