Manikandan
Manikandan

Reputation: 699

How to handle the exception in OpenFire Restapi in php?

Currently I installed openfire in my server and started to use it. I tried to create a user in openfire using Restapi . And I got output as I expected.

Now i tried to create a user with the username "abcdef" which is already existed. I want a message that says "username already exists" but instead I get an exception.

The exception:

Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error response [url] http://myip:9090/plugins/restapi/v1/users [status code] 409 [reason phrase] Conflict' in /var/www/html/open_fire_internal/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:88

The message from the exception:

Client error response [url] http://myip:9090/plugins/restapi/v1/users [status code] 409 [reason phrase] Conflict

Stack trace:

#0 [...]/vendor/guzzlehttp/guzzle/src/Subscriber/HttpError.php(33): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Message\Request), Object(GuzzleHttp\Message\Response))
#1 [...]/vendor/guzzlehttp/guzzle/src/Event/Emitter.php(109): GuzzleHttp\Subscriber\HttpError->onComplete(Object(GuzzleHttp\Event\CompleteEvent), 'complete')
#2 [...]/vendor/guzzlehttp/guzzle/src/RequestFsm.php(91): GuzzleHttp\Event\Emitter->emit('complete', Object(GuzzleHttp\Event\CompleteEvent))
#3 [...]/vendor/guzzlehttp/guzzle/src/RequestFsm.php(132): GuzzleHttp\RequestFsm->__invoke(Object(GuzzleHttp\Transaction))in /var/www/html/open_fire_internal/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 88

So can anyone help me to overcome from this issue?

Upvotes: 0

Views: 442

Answers (1)

Ahmed Khan
Ahmed Khan

Reputation: 516

First check whether the user is exists or not than add them

$user = $api->getuser($username);

if(!$user)
{
$result = $api->addUser('Username', 'Password', 'Real Name', '[email protected]', array('Group 1'));

// Check result if command is succesful
if($result) {
    // Display result, and check if it's an error or correct response
    echo ($result['result']) ? 'Success: ' : 'Error: ';
    echo $result['message'];
} else {
    // Something went wrong, probably connection issues
}
}
else
{

echo 'user already exists';
}

Upvotes: 1

Related Questions