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.
 

Asset API into mysql (using php?)

Author
Big FatWallet
Synergie Industries
#1 - 2012-08-28 18:51:06 UTC
Hi guys,

Noob coder and pulling my hair out so appreciate any help.

I'm trying to import my asset api into a mysql database using php. I've done a bit of research and tried some code but to no avail.




$url="https://api.eveonline.com/char/AssetList.xml.aspx?keyID=xxxxx&vCode=xxxxx";

//Begins connecting to eve api

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

//Create XML Parser
$xml = new SimpleXMLElement(curl_exec($ch));
curl_close($ch);

//Parse the file

    foreach ($xml->xpath('//row') as $row) {
       {           
            mysql_query('INSERT into player_inventory (player_name, item_id, qty) Values ('$user', $row['typeid'], $row['quantity']') or die(mysql_error());     

        }
    }




$user comes from a cookie already stored on the webpage.

Many
Khorkrak
KarmaFleet
Goonswarm Federation
#2 - 2012-08-28 20:14:36 UTC
What error message are you receiving? It appears that you're missing the characterID parameter. So

$url="https://api.eveonline.com/char/AssetList.xml.aspx?keyID=xxxxx&vCode=xxxxx&characterID=xxxxxxxxxx";

I am now slightly irate with you for making me write a line of disgusting PHP. Please don't do it again and next time write code in Python.

Developer of http://www.decloaked.com and http://sourceforge.net/projects/pykb/

Big FatWallet
Synergie Industries
#3 - 2012-08-28 20:37:54 UTC
I've never written a single line of python sorry :(

I did have a characterID variable on the end of the URL but removed it by accident when pasting into here.

I get the following error:

Parse error: syntax error, unexpected T_VARIABLE in /home/bigfatw/public_html/td/charimport.php on line 20

This relates to the following line:

mysql_query('INSERT into player_inventory (player_name, item_id, qty) Values ('$user', $row['typeid'], $row['quantity']') or die(mysql_error());

assume this is something to do with the '$user'

Thanks again for your help. Appreciate it.
Desmont McCallock
#4 - 2012-08-28 20:58:35 UTC  |  Edited by: Desmont McCallock
Shouldn't it be $row['typeID'] ? I'm no PHP coder, so I'm not familiar if PHP is case sensitive on XML parsing.
Khorkrak
KarmaFleet
Goonswarm Federation
#5 - 2012-08-28 21:08:02 UTC  |  Edited by: Khorkrak
Desmont is correct but your error is likely even before you hit that problem. Try using double quotes for the entire SQL statement leaving single quotes inside where necessary.

mysql_query("INSERT into player_inventory (player_name, item_id, qty) Values ($user, $row['typeID'], $row['quantity'])") or die(mysql_error());

Developer of http://www.decloaked.com and http://sourceforge.net/projects/pykb/

Big FatWallet
Synergie Industries
#6 - 2012-08-28 21:11:28 UTC
Changed and I now get the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/bigfatw/public_html/td/charimport.php on line 20


Khorkrak
KarmaFleet
Goonswarm Federation
#7 - 2012-08-28 21:12:43 UTC
Try again - edited to add in missing right parenthesis.

Developer of http://www.decloaked.com and http://sourceforge.net/projects/pykb/

Big FatWallet
Synergie Industries
#8 - 2012-08-28 21:19:49 UTC  |  Edited by: Big FatWallet
No errors but the table does not populate.

Is there an easier way to take the asset list and insert the typeID and quantity (along with a user from a cookie) into a mysql database?
Desmont McCallock
#9 - 2012-08-28 21:40:25 UTC
A successful programmer is also a good debugger.
Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#10 - 2012-08-28 22:20:49 UTC  |  Edited by: Steve Ronuken
I put together a corp asset loader a while back, when yapeal didn't maintain the locations the way I wanted, for my stockpile mangement.

Here it is. It's missing all the database definitions, and might not work properly for non corp assets. But it may be enough to give you a clue.

http://pastebin.com/gKnyq2K1


Oh, and var_dump is your friend. It'll print out everything in a variable.

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Big FatWallet
Synergie Industries
#11 - 2012-08-28 22:23:56 UTC
Managed to fix it.

Thanks for your help guys. I was close....


mysql_query("INSERT into player_inventory (player_name, item_id, qty) Values ($user, $row['typeID'], $row['quantity'])") or die(mysql_error());

had to change the array variables to the following

mysql_query("INSERT into player_inventory (player_name, item_id, qty) Values ($user, '$row[typeID]', '$row[quantity]')") or die(mysql_error());
Dragonaire
Here there be Dragons
#12 - 2012-08-31 02:52:39 UTC
I'd split this up and check what you are actually getting from the API

$xml = new SimpleXMLElement(curl_exec($ch));

IE

$result = curl_exec($ch);
$xml = new SimpleXMLElement($result);

You should also put in some error checking code after both assignments as they can both fail and will return FALSE on errors.

$result = ...
if ($result === FALSE) {
print 'I failed!' . PHP_EOL;
}
$xml = ...

In addition I suggest you move to either mysqli or PDO as mysql is very old and on it's way out SOON (TM) but glad you figured out what was wrong.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#13 - 2012-08-31 09:11:54 UTC
PDO is the way to go.

Sure, in /this/ case sql injection isn't too likely. But the ability to use bind variables in your SQL is a very good thing. And it helps to stay in practive.

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter