These forums have been archived and are now read-only.

The new forums are live and can be found at https://forums.eveonline.com/

EVE Technology Lab

 
  • Topic is locked indefinitely.
 

Can anyone tell me what i'm doing wrong with my PHP? (apart from the being a noob bit i know tha

Author
Naran Eto
Caldari Provisions
Caldari State
#1 - 2011-09-23 14:21:31 UTC  |  Edited by: Naran Eto
I'm trying to teach myself a bit of PHP and practising with the Eve Online XML's, i've kind of hit a wall though with this one though, i've read tons of tutorials and guides and it seems right, but i'm just getting errors.

I'm trying to access this XML http://api.eveonline.com/eve/AllianceList.xml.aspx and parse it with the following PHP ...

Quote:


?php

$data = file_get_contents('http://api.eveonline.com/eve/AllianceList.xml.aspx');
$xml = SimpleXMLElement($data);

print($xml->result->rowset[1]->row->name);
print($xml->result->rowset[1]->row->shortName);
print($xml->result->rowset[1]->row->memberCount);

?



Had to leave the open and close markers out because the forum tries to parse it as html and freaks out.

I'm trying to basically list the name, short name and number of members of an the first allaince on the XML list, but it's not going well, all i get is the following error ...

Fatal error: Call to undefined function SimpleXMLElement() in /home/xtqyi/public_html/testing/index.php on line 4

Anyone able to show me what i've done wrong?
KhalimAmnov
Almost Dangerous
Wolves Amongst Strangers
#2 - 2011-09-23 16:24:37 UTC
A bit of Googling has come up with the following:

One possibility is that you're using SimpleXML incorrectly. Documentation can be found here.

According to that, you'd want:

$xml = new SimpleXMLElement($data);

I'd say give that a try first and see if it works. If it doesn't, familiarize yourself more with the documentation for SimpleXML.


If that still doesn't work, you could try reading this.

Basically, try printing out a call to phpinfo() and see if SimpleXML is loaded. You'll want to look for a reference to simplexml.so, it seems. Depending on your version of PHP installed on the server, SimpleXML may not be installed/loaded.


If it still doesn't work, you might try this alternative solution.
Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#3 - 2011-09-23 16:55:46 UTC
You're trying to use it as a function, rather than creating a new object . Aside from that, it's not a nice object to work with. as the alliance list is massive, I'd suggest trying one of the smaller ones to get the hang of how it breaks it down. like the character api one. Then var_dump it.





Quote:


?php

$data = file_get_contents('https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID=90926985');
$xml = new SimpleXMLElement($data);
var_dump($xml);


?




From the way it breaks down, something like the code below is needed to print out a character name.

Quote:


?php

$data = file_get_contents('https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID=90926985');
$xml = new SimpleXMLElement($data);
#var_dump($xml);


print $xml->result->{'characterName'}->{0};


?



In other news:

Quote:

?php


$data = file_get_contents('http://api.eveonline.com/eve/AllianceList.xml.aspx');
$xml = new SimpleXMLElement($data);


print $xml->result->rowset->row->{1}['name']."\n";
print $xml->result->rowset->row->{1}['shortName']."\n";
print $xml->result->rowset->row->{1}['memberCount']."\n";

?




Should do what you're looking for.

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Naran Eto
Caldari Provisions
Caldari State
#4 - 2011-09-23 21:41:25 UTC  |  Edited by: Naran Eto
OK, managed to work it all out YAY!

Thanks for the help guys, i expanded on what you guys told me and managed to work out how to put it all in a table and loop through each rowset to get a table form the xml.

Not bad for someoen who only started teaching herself xml 12hrs ago :D

Quote:


< ?php


$data = file_get_contents('http://api.eveonline.com/eve/AllianceList.xml.aspx');
$xml = new SimpleXMLElement($data);
echo"< table cellspacing='3' >";
$i = 0;
while ($xml->result->rowset->row[$i])
{
echo" tr
< td > {$xml->result->rowset->row{$i}['name']}< /td >
< td > {$xml->result->rowset->row{$i}['shortName']}< /td >
< td > {$xml->result->rowset->row{$i}['memberCount']}< td >
< /tr >";
$i++;


}
echo "< /table >;
? >

Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#5 - 2011-09-23 22:37:18 UTC
One small thing to consider. you might want to get it to cache to disk rather than loading it all each time. It's a relatively big file, so if it's more than just you, it'll load your server down.

just check to see if the file exists, and what it's date is. if it's recent, use that, otherwise do the remote get and dump a copy to your cache file. (assuming it's valid at that time)

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Johnathan Roark
Quantum Industries
#6 - 2011-09-24 04:21:08 UTC
Naran Eto wrote:
OK, managed to work it all out YAY!

Thanks for the help guys, i expanded on what you guys told me and managed to work out how to put it all in a table and loop through each rowset to get a table form the xml.

Not bad for someoen who only started teaching herself xml 12hrs ago :D

Quote:


< ?php


$data = file_get_contents('http://api.eveonline.com/eve/AllianceList.xml.aspx');
$xml = new SimpleXMLElement($data);
echo"< table cellspacing='3' >";
$i = 0;
while ($xml->result->rowset->row[$i])
{
echo" tr
< td > {$xml->result->rowset->row{$i}['name']}< /td >
< td > {$xml->result->rowset->row{$i}['shortName']}< /td >
< td > {$xml->result->rowset->row{$i}['memberCount']}< td >
< /tr >";
$i++;


}
echo "< /table >;
? >



I suggest using xpath:

foreach($xml->xpath('//row') as $row)
{
//stuff
}

also, I'd look into using cURL. It errors out better, its going to happen while working with the api, and it gives you more options such as using POST rather then GET to submit parameters to the API which is what ccp prefers.

Long term, I also suggest looking at using one of the libraries such as yapeal or pheal. Practice doing it from scratch, but its much quicker and less error prone to use one of the libraries for any projects you may do.

EVEVERIFY - A recruiting API Verification and Audit Tool

Also try out Yapeal for your php api needs

Naran Eto
Caldari Provisions
Caldari State
#7 - 2011-09-24 12:54:33 UTC
Hi Johnathon thanks for the tips, i looked into cURL but it just won't install on either of my machines, tells me i'm missing dll's whic i do have grrr... as to the libraries i also loked at them, but to be honest i haven't a clue what to do with them, i'm only just starting to learn basic php from a web php for dummies book and looking at those libraries was a serious wtf moment for me. I just don't know what they're meant to do or how to use them.