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?
PHP parsing
The SimpleXML API introduced with PHP5 makes working with XML actually pretty easy, but you won't get around reading at least some manuals ;-)
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 ?>
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>
Okay, so it's blank. I think I understand why. I'll copy this to my localhost installation see what I can do.
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.
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.
$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.
Thanks, dargonaut. I was just learning at the time (and still am) so I stuck with XML while just figuring this stuff out.
Please Log In to post.
Log in to comment