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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料