PHP parsing

Avatar image for cook66
Cook66

281

Forum Posts

1058

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#1  Edited By Cook66

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
zerok

216

Forum Posts

223

Wiki Points

0

Followers

Reviews: 5

User Lists: 17

#2  Edited By zerok

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
Cook66

281

Forum Posts

1058

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#3  Edited By Cook66

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
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#4  Edited By Notso

@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
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#5  Edited By Notso

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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#6  Edited By LordAndrew

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

Avatar image for notso
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#7  Edited By Notso

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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#8  Edited By LordAndrew

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

Avatar image for notso
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#9  Edited By Notso

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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#10  Edited By LordAndrew

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

Avatar image for notso
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#11  Edited By Notso

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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#12  Edited By LordAndrew

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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#13  Edited By LordAndrew
@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
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#14  Edited By Notso
Avatar image for lordandrew
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#15  Edited By LordAndrew

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
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#16  Edited By Notso

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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#17  Edited By LordAndrew
$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
Darg

34

Forum Posts

1

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#18  Edited By Darg

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

Avatar image for notso
Notso

24

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#19  Edited By Notso

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
ModestINK

2

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#20  Edited By ModestINK

@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
LordAndrew

14609

Forum Posts

98305

Wiki Points

0

Followers

Reviews: 0

User Lists: 36

#21  Edited By LordAndrew
@ModestINK: Ah, glad to see you got that worked out. :)