gidkom / php-openfire-restapi

Remotely manage the Openfire server using Rest Api
MIT License
57 stars 37 forks source link

Handle GuzzleHttp\Exception inside client #3

Closed C-Duv closed 8 years ago

C-Duv commented 9 years ago

When trying to access a nonexisting group with:

$apiClient->getGroup('nonexistent_foobar_group');

I get a GuzzleHttp\Exception\ClientException:

PHP Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error response [url] https://openfire.example.com:9091/plugins/restapi/v1/groups/nonexistent_foobar_group [status code] 404 [reason phrase] Not Found' in .../myproject/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:89 Stack trace: 0 .../myproject/vendor/guzzlehttp/guzzle/src/Subscriber/HttpError.php(33): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Message\Request), Object(GuzzleHttp\Message\Response)) 1 .../myproject/vendor/guzzlehttp/guzzle/src/Event/Emitter.php(109): GuzzleHttp\Subscriber\HttpError->onComplete(Object(GuzzleHttp\Event\CompleteEvent), 'complete') 2 /home/claude-duv in .../myproject/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 89

I feel like 404 responses and (GuzzleHttp\Exception in general) should be dealt by OpenFireRestApi itself.

gidkom commented 9 years ago

thanks a lot. I will work on that

varunjoshi12 commented 9 years ago

How to create group on openfire and then add member on group using url

I want to create group on open fire and then add member on that group using url. i have create the user on open fire by using this url http://myhostname:port/plugins/userService/userservice?type=add&secret=mysecretkey&username=$username&password=$password&name=$name&email=$email

Please help me to create group and add users on that group and i have used this url to create group http://myhostname:port/plugins/userService/userservice?type=add&secret=mysecretkey&name='spalchat'&description='parent'

but it throws an exception:IllegalArgumentException

gidkom commented 9 years ago

First of all user service plugin is deprecated. You should use the Rest API plugin. Go through the docs on its usage (http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html)

varunjoshi12 commented 9 years ago

yes i have read this docs,but my question is there any url to create group on openfire because i have created the user on openfire by URL so i have to use user service plugin is it possible?

dibwas commented 8 years ago

Hi Gidkom,

I have http://www.igniterealtime.org/projects/openfire/plugins/restapi installed on openfire. But still I am getting the below error:

[07-Dec-2015 15:41:36 America/Detroit] PHP Fatal error: Uncaught exception 'GuzzleHttp\Ring\Exception\ConnectException' with message 'cURL error 7: See http://curl.haxx.se/libcurl/c/libcurl-errors.html' in /home/websoftt/public_html/portfolio/sites/jugaado/chat/vendor/guzzlehttp/ringphp/src/Client/CurlFactory.php:126 Stack trace:

0 /home/websoftt/public_html/portfolio/sites/jugaado/chat/vendor/guzzlehttp/ringphp/src/Client/CurlFactory.php(91): GuzzleHttp\Ring\Client\CurlFactory::createErrorResponse(Object(GuzzleHttp\Ring\Client\CurlMultiHandler), Array, Array)

1 /home/websoftt/public_html/portfolio/sites/jugaado/chat/vendor/guzzlehttp/ringphp/src/Client/CurlMultiHandler.php(244): GuzzleHttp\Ring\Client\CurlFactory::createResponse(Object(GuzzleHttp\Ring\Client\CurlMultiHandler), Array, Array, Array, Resource id #60)

2 /home/websoftt/public_html/portfolio/sites/jugaado/chat/vendor/guzzlehttp/ringphp/src/Client/CurlMultiHandler.php(136): GuzzleHttp\Ring\Client\CurlMultiHandler->processMessages()

3 /home/websoftt/public_html/portfolio/sites/jugaado/chat/vendor/guz in /home/websoftt/public_html/portfolio/sites/jugaado/chat/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 49

Below is my code:

<?php

include "vendor/autoload.php";

// Create the Openfire Rest api object $api = new Gidkom\OpenFireRestApi\OpenFireRestApi;

// Set the required config parameters $api->secret = "SECRET"; $api->host = "MY HOST"; $api->port = "9090"; // default 9090

// Optional parameters (showing default values)

$api->useSSL = false; $api->plugin = "/plugins/restapi/v1"; // plugin

// Add a new user to OpenFire and add to a group $result = $api->addUser('dibsisbest@openfire', 'password', 'Real Name', 'dibsisbest@jugaado.com', array());

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

?>

pgstath commented 8 years ago

@dibwas please see this issue The error could be caused by the @ character in the jid node, it must be escaped as \40

Rajeshwar90 commented 8 years ago

Hi Gidkom,

The issue did not resolve after using \40 in place of @ in email field.Can you please let me know how to come out from this given below exception.

PHP Fatal error: Uncaught exception 'GuzzleHttp\Exception\ServerException' with message 'Server error response [url] http://openfire.smartchk.net:9090/plugins/restapi/v1/users [status code] 500 [reason phrase] Server Error' in /var/www/html/XMPP_POC/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:89

0 /var/www/html/XMPP_POC/vendor/guzzlehttp/guzzle/src/Subscriber/HttpError.php(33): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Message\Request), Object(GuzzleHttp\Message\Response))

1 /var/www/html/XMPP_POC/vendor/guzzlehttp/guzzle/src/Event/Emitter.php(109): GuzzleHttp\Subscriber\HttpError->onComplete(Object(GuzzleHttp\Event\CompleteEvent), 'complete')

2 /var/www/html/XMPP_POC/vendor/guzzlehttp/guzzle/src/RequestFsm.php(91): GuzzleHttp\Event\Emitter->emit('complete', Object(GuzzleHttp\Event\CompleteEvent))

paulewoodhouse commented 8 years ago

can use php exception handling to work round this one... I wrote a script to add all users as buddies to all other users, and use a try - catch block as a quick and dirty way to ignore conflicts caused when a user is already in someones roster...

the exception message should also give you a better clue whats happened..

$result = $api->getUsers();

if($result['status']){ foreach ($result['message']->user as $value) { if($value->username != $q){ try { $setresult = $api->addToRoster($q, $value->username.'@'.$jidfinish); echo $value->username." added to : ".$q."
"; } catch (Exception $e) { echo "Caught Exception : ".$e->getMessage(), "<br >"; } } } unset($value); } else { echo 'Error'; print_r ($result['message']); }