dqo88037 2016-01-08 07:16
浏览 114
已采纳

如何在PHP中的OpenFire Restapi中处理异常?

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?

  • 写回答

1条回答 默认 最新

  • doutizong8099 2016-01-08 10:05
    关注

    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@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';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化