Page 1 of 2
ManiaConnect doesn't work (bis)
Posted: 03 Nov 2011, 22:22
by Kimi3
Hello all !
For my Manialink Motor, I still have an error on my script for Maniaconnect, and still don't understand why. Bo$$ and me tried a lot of things !
Code: Select all
<?php //Plages erreur : 800-809
// Authentification avec ManiaConnect :
require_once $dir . '/maniaplanet-ws-sdk/libraries/autoload.php';
define('API_USERNAME', $api_username);
define('API_PASSWORD', $api_password);
define('SCOPE', '');
try
{
$trackmania = new \Maniaplanet\WebServices\ManiaConnect\Player(API_USERNAME, API_PASSWORD);
// URLs to log in and out
$loginURL = $trackmania->getLoginURL(SCOPE);
$logoutURL = $trackmania->getLogoutURL();
if(isset($_POST['logout']))
{
$trackmania->logout();
header('Location: '.$logoutURL);
exit;
}
// Retrive player information. If the user is not logged in, it will return false
$player = $trackmania->getPlayer();
}
catch(\Maniaplanet\WebServices\Exception $e);
{
$player = null;
}
$_SESSION['playerlogin'] = $player->login;
$_SESSION['nickname'] = $player->nickname;
$_SESSION['path'] = $player->path;
$_SESSION['player_id'] = $player->id;?>
The window to allow player to identify works, but then Invalid Manialink File.. ! The error is that the $player->login; variable return nothing. Do you have an idea ?
Re: ManiaConnect doesn't work
Posted: 03 Nov 2011, 22:36
by TMarc
Is debugging possible? so you can display the $player structure with all its contents?
Re: ManiaConnect doesn't work
Posted: 03 Nov 2011, 23:37
by m4rcel
I think, the problem is that you catch any Exceptions, but you don't handle them the correct way.
Imagine you have the following situation:
Let ManiaConnect throw any error (the concrete error does not matter). You catch the raised exception (so far correct), and you assign null to $player... and right after that, you try to access this $player with ->id etc, which will generate PHP errors ("Cannot access field of null value" or sth like that), making your Manialink invalid.
I assume, that exactly this is your current situation.
My suggestion: Improve the error handling, printing the real error as nice message into the ManiaLink (because if an Exception is raised, _always_ something went wrong), and only if $player is really an object (check with is_object($player)), access its properties.
(Stupid question: Does $api_username and $api_password contain the username/password of your API user? ^^)
Re: ManiaConnect doesn't work
Posted: 04 Nov 2011, 06:48
by Boss-Bravo
It's may be that, we don't verified that $player is null.
m4rcel wrote:(Stupid question: Does $api_username and $api_password contain the username/password of your API user? ^^)
Yes, they containt username and password

. It's not the main file, in the main file we include this file "connection.php" and before, we define the variables $api_username, $api_password and $dir correctly.
Thanks for your help. We will test this.

Re: ManiaConnect doesn't work
Posted: 04 Nov 2011, 08:30
by Kimi3
Thanks ! We'll try this tonight

Re: ManiaConnect doesn't work
Posted: 05 Nov 2011, 08:26
by Boss-Bravo
We tested if $player is null, is not null, for tried this, i maked that :
Code: Select all
catch(\Maniaplanet\WebServices\Exception $e);
{
$player = null;
echo 'player NULL';
}
echo 'player non NULL';
$_SESSION['playerlogin'] = $player->login;
$_SESSION['nickname'] = $player->nickname;
$_SESSION['path'] = $player->path;
$_SESSION['player_id'] = $player->id;?>
And i see only 'player non NULL', so, $player is not null. After that, i tried to print_r($player) before SESSION, and it doesn't work, i don't understand where the error is. I think $player is not defined, but why ?
Thanks to help us.

Re: ManiaConnect doesn't work
Posted: 05 Nov 2011, 10:01
by m4rcel
Do you keep in mind, that
$trackmania->getPlayer(); can also return
false? (If the player does not have given the permission to his or her data, getPlayer() returns false instead of an object.)
That's why I said you should check with is_object($player), if $player is actual an object, before accessing it

Re: ManiaConnect doesn't work
Posted: 05 Nov 2011, 15:09
by gouxim
The problem is that you catch exceptions, and then after that you use the $player var as if it was always an object.
But if you're not logged in yet, $player will be null (because of the catch block before) hence $player-login won't work.
You need to check if $player is null or not to handle both cases (logged in or not), just like we did in the example:
http://code.google.com/p/maniaplanet-ws ... dex.php#50
Re: ManiaConnect doesn't work
Posted: 06 Nov 2011, 10:19
by Boss-Bravo
I'm stupid, I did it for my manialink, but I forgot to do for m2
It Works

Thanks to you Gouxim

Re: ManiaConnect doesn't work
Posted: 06 Nov 2011, 11:07
by Kimi3
Thanks all for your help!
