【问题标题】:How to handle the exception in OpenFire Restapi in php?如何在 php 中处理 OpenFire Restapi 中的异常?
【发布时间】:2016-04-12 19:18:46
【问题描述】:

目前我在我的服务器上安装了 openfire 并开始使用它。我尝试使用 Restapi 在 openfire 中创建用户。我得到了预期的输出。

现在我尝试使用已经存在的用户名“abcdef”创建用户。我想要一条显示“用户名已存在”的消息,但我得到了一个异常。

例外:

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

来自异常的消息:

客户端错误响应 [url] http://myip:9090/plugins/restapi/v1/users [状态码] 409 [原因短语] 冲突

堆栈跟踪:

#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

那么任何人都可以帮助我克服这个问题吗?

【问题讨论】:

  • 你能出示你的代码吗!
  • include "../vendor/autoload.php"; 987654325 $api = new Gidkom\OpenFireRestApi\OpenFireRestApi; 987654327 $result = $api->addUser('kandan', 'mani'); 987654329 if($result['status']) { 987654331 echo 'Success: '; 987654333 } else { 987654335 echo 'Error: '; 987654337 @ }
  • 当用户名在 openfire 中不存在时,我会收到成功消息,但当用户名在 openfire 中存在时,我会收到该异常。

标签: php exception openfire


【解决方案1】:

先检查用户是否存在,然后添加他们

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

if(!$user)
{
$result = $api->addUser('Username', 'Password', 'Real Name', 'email@email.tld', 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';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多