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.
 

PHP/API Question

Author
Mr Twinkie
Republic Military School
Minmatar Republic
#1 - 2015-09-22 07:11:21 UTC  |  Edited by: Mr Twinkie
I'm trying to check an api for a character id using PHP.

This is the code I'm using

http://pastebin.com/VYbNVtgY (even in code tags this **** forum yells at me for posting code)

Obviously $id is defined earlier, but for some reason this isn't working?


Basically just need to make sure the character id exists somewhere in the xml
Bloemkoolsaus
Deep Core Mining Inc.
Caldari State
#2 - 2015-09-22 09:07:11 UTC
if ($charidcheck = NULL)

You are assigning null to $charidcheck, the if will return true because it'll be succesfully assigned.

Instead, use this to check if the var is null:
if ($charidcheck == NULL)


Note that php can get a bit weird with null because of the way it handles data types. Empty strings for exemple will also qualify as null.
If you want to check if it's actually NULL instead of just empty, use:
if ($charidcheck === NULL)


Mr Twinkie
Republic Military School
Minmatar Republic
#3 - 2015-09-22 09:44:37 UTC
Bloemkoolsaus wrote:
if ($charidcheck = NULL)

You are assigning null to $charidcheck, the if will return true because it'll be succesfully assigned.

Instead, use this to check if the var is null:
if ($charidcheck == NULL)


Note that php can get a bit weird with null because of the way it handles data types. Empty strings for exemple will also qualify as null.
If you want to check if it's actually NULL instead of just empty, use:
if ($charidcheck === NULL)




Bah... literally wasted 2 hours on this and going from = to == fixed it haha. Thanks!
Yogi Berimor
Monkey Attack Squad
Goonswarm Federation
#4 - 2015-09-22 23:22:34 UTC  |  Edited by: Yogi Berimor
The best way to avoid such mistakes in the future is to use reverse assertion syntax like this:

if(NULL == $var) {
//some code
}

This will raise an error should you put an assigment (" = ") instead of assertion (" == " / " === ").

It might take some time to get used to it but it is worth the effort. It is also considered as best practice
Mr Twinkie
Republic Military School
Minmatar Republic
#5 - 2015-09-22 23:52:36 UTC
Yogi Berimor wrote:
The best way to avoid such mistakes in the future is to use reverse assertion syntax like this:

if(NULL == $var) {
//some code
}

This will raise an error should you put an assigment (" = ") instead of assertion (" == " / " === ").

It might take some time to get used to it but it is worth the effort. It is also considered as best practice



Ya still getting into the good habits, self taught myself php while deployed over the winter so my code isn't the cleanest.
Mr Twinkie
Republic Military School
Minmatar Republic
#6 - 2015-09-22 23:57:44 UTC
Another question. I'm trying to setup a cron to check if the character is in corp using the charid and the characterinfo.xml.aspx

The charid is stored in a mysql db and my issue is I'm not sure how to get the cron to run down the rows and check every single row.

The current code is - http://pastebin.com/JiQmdaDq

will it automatically loop thru? or am I missing something to make it do that
Yogi Berimor
Monkey Attack Squad
Goonswarm Federation
#7 - 2015-09-23 02:35:10 UTC  |  Edited by: Yogi Berimor
Add a new column to your characters table named 'UpdatedAt'(or something like that) of type "DateTime".
Change

$query = 'SELECT charid FROM services.members;';

To

$query = 'SELECT charid FROM services.members ORDER BY UpdatedAt DESC;';

(You can even consider adding a LIMIT here)

Then wrap your other stuff in a loop

// Corp Check

while( $row = mysqli_fetch_row($result) ) {

                                        $url = 'https://api.eveonline.com/eve/CharacterInfo.xml.aspx';
                                        $url .= '?characterID=' . urlencode($result);
                                       
                                        $xml = makeApiRequest($url);
                                        $corpid = (int) $xml->result->corporationID;
                                       
                                        if ($corpid == '875512489') { $query2 = 'UPDATE services.members SET role = "MAMBA" WHERE charid = $result LIMIT 1'; }
                                        else { $query2 = 'UPDATE services.members SET role = "OOC" WHERE charid = $row[0] LIMIT 1'; }

                                        $result2 = mysqli_query($con,$query2);

  if (!$result2) {
    die('Invalid query: ' . mysql_error());
}

}

Havent tested this code, might be some syntax issues, but you get the idea.

I really recommend you to read about PDO and use that instead of mysqli bs.
Also, have a look at https://github.com/3rdpartyeve/phealng - this lib handles all the boring stuff and is pretty easy to use.


P.S. This reminds me i need to speed up the release of my Starlight project.
Mr Twinkie
Republic Military School
Minmatar Republic
#8 - 2015-09-23 03:15:21 UTC
Throws

"Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[0] LIMIT 1' at line 1"

if I put $row[0] in quotes it no longer throws the error but it also doesn't do anything in the db.
Jack Hayson
The Scope
Gallente Federation
#9 - 2015-09-23 10:08:21 UTC
Mr Twinkie wrote:
Throws

"Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[0] LIMIT 1' at line 1"

if I put $row[0] in quotes it no longer throws the error but it also doesn't do anything in the db.


If you want php to interpret variables inside strings you need to use double quotes " not single quotes '.

so either
$query2 = "UPDATE services.members SET role = \'OOC\' WHERE charid = $row[0] LIMIT 1";

or
$query2 = 'UPDATE services.members SET role = \'OOC\' WHERE charid = '.$row[0].' LIMIT 1';
Mr Twinkie
Republic Military School
Minmatar Republic
#10 - 2015-09-24 04:00:13 UTC
Jack Hayson wrote:
Mr Twinkie wrote:
Throws

"Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[0] LIMIT 1' at line 1"

if I put $row[0] in quotes it no longer throws the error but it also doesn't do anything in the db.


If you want php to interpret variables inside strings you need to use double quotes " not single quotes '.

so either
$query2 = "UPDATE services.members SET role = \'OOC\' WHERE charid = $row[0] LIMIT 1";

or
$query2 = 'UPDATE services.members SET role = \'OOC\' WHERE charid = '.$row[0].' LIMIT 1';


Ya figured it out after like 3 hours :P
BB-8
Doomheim
#11 - 2015-09-27 15:37:20 UTC
would be cool if there was an php dev channel to discuss issues like this so that when the guy wastes 2 hours over a syntax error he doesnt have to wait even longer for a forum post.

#eve-dev on coldfront is dead.
Vast Space
Perkone
Caldari State
#12 - 2015-09-27 18:19:22 UTC
BB-8 wrote:
would be cool if there was an php dev channel to discuss issues like this so that when the guy wastes 2 hours over a syntax error he doesnt have to wait even longer for a forum post.

#eve-dev on coldfront is dead.


www.stackoverflow.com
www.w3schools.com/php/
Dragonaire
Here there be Dragons
#13 - 2015-10-01 20:30:41 UTC  |  Edited by: Dragonaire
Something I would suggest to anyone especially learning a new programming language is to find a good editor with syntax highlighting it'll make a big difference in finding errors like these. Even better is a good IDE which can include plug-ins for code inspections which can catch even more things and help enforce good coding practices. For a simple code editer I like Notepad++(Windows) but an IDE is much better and a couple I've used are Komodo-edit (free, cross-platform) and the one I current use which is PhpStorm (free trail, cross-platform) from JetBrain. It's code style settings and inspections are very good. It also understands more than just PHP and will help with the SQL syntax etc as well. For some coding standards for PHP you should look at the PSR ones. PHPStorm even let's you set it up to correct the formatting for you on commits etc.

Also as someone pointed out except if you are just doing for the learning it's better not to be re-inventing the wheel when it comes to the Eve API. The are existing libraries like Pheal-ng or my own Yapeal which deal with all the misc cruft and inconsistent there are in the APIs so you don't have to. Even if your just trying to learn you would probably find looking at other people's code useful especially where we're solving the same problems you are trying to work on.

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