NicklasWallgren / PokemonGoAPI-PHP

Pokemon Go API PHP library
BSD 2-Clause "Simplified" License
130 stars 51 forks source link

Get Exception Login in variable #136

Closed scrabionau77 closed 7 years ago

scrabionau77 commented 7 years ago

How can i get exception message in login? if i use wrong username or password and i do:

// Retrieve the profile
$profile = $pokemonGoApi->getProfile();
print_f($profile);

result is: Fatal error: Uncaught exception 'NicklasW\PkmGoApi\Authentication\Exceptions\AuthenticationException' with message 'Your username or password is incorrect. You have 4 attempts left before you will be locked out of your account for 15 minutes.'

how to get $message = 'Your username or password is incorrect. You have 4 attempts left before you will be locked out of your account for 15 minutes.' ? Thanks to all!

DrDelay commented 7 years ago

This may be a good read for you: https://secure.php.net/manual/en/language.exceptions.php

So, basically you want to try to get-the-Profile() and catch a possibly occuring AuthenticationException. If you catch one, you want to get-it's-Message():

$profile = null;
try {
  $profile = $pokemonGoApi->getProfile();
} catch (\NicklasW\PkmGoApi\Authentication\Exceptions\AuthenticationException $authEx) {
  $message = $authEx->getMessage();
}

You won't run into a fatal error when you catch the exception, so you'd probably want to terminate your application yourself (in the catch block, you only enter it in case of errors).

scrabionau77 commented 7 years ago

Thanks DrDelay! But doesn't works! My code:

......
$config = new Config();
$config->setUser("RIGHT_USER");
$config->setPassword("WRONG_PWD");
$config->setProvider(Factory::PROVIDER_PTC);
$manager = Factory::create($config);
$application = new ApplicationKernel($manager);
$pokemonGoApi = $application->getPokemonGoApi();

$profile = null;
try {
  $profile = $pokemonGoApi->getProfile();
} catch (NicklasW\PkmGoApi\Authentication\Exceptions\AuthenticationException $authEx) {
  //$message = $authEx->getMessage();
  print_r($authEx->getMessage());
}

i get blank page!

DrDelay commented 7 years ago

But you get no fatal error ... so you run in the catch block (or there is no error, but the credentials look wrong :-D ) -> debug it. var_dump($authEx). \Exception::getMessage() returns a string, don't know how print_r works with strings, use echo/print instead.

scrabionau77 commented 7 years ago

i don't know why, but this 3 solutions

print($authEx);
var_dump($authEx);
echo $authEx; 

(used one by one) inside catch returns blank! Password is really wrong!

DrDelay commented 7 years ago

I'm sorry to say it, but I shouldn't have to tell you about basic coding and debugging a PHP program. It's not part of this repository.

Empty page you say, even with var_dump. var_dump is guaranteed to output something, even if it is only NULL. So you probably have an error somewhere that prevents execution but is not rendered on the screen. Turn on all errors in your development environment, if you don't know how put this at your programs entry point:

error_reporting(E_ALL);
ini_set('display_errors', true);