Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a chat room and add users into the room in openfire using PHP

I'm new to XMPP server. I want to make multi user chat application.

I have installed Openfire and one to one chat is working properly but i'm not able create a conference(chat) room and add users into the room using PHP for multi user chat.

Although I have installed MUC service plugin available in openfire but I dont know how to implement the MUC Service REST/HTTP with PHP.

Can anybody have some sample PHP script for MUC service to create chat room and adding users into the chat room?

Thanks in Advance

like image 661
Vaiju Sajjan Avatar asked Nov 09 '22 19:11

Vaiju Sajjan


1 Answers

Base on help information:

Basic HTTP Authentication

All REST Endpoint are secured by Basic HTTP Authentication.

To access the endpoints is that required to send the Username and Password of a Openfire Admin account in your header request.

E.g. Header: Authorization: Basic YWRtaW46MTIzNDU= (username: admin / password: 12345)

Example for Jersey Client

Client c = Client.create(); c.addFilter(new HTTPBasicAuthFilter(user, password));

POST /mucservice/chatrooms Endpoint to create a new chat room.

Payload: Chatroom Return value: HTTP status 201 (Created)

Possible parameters

Parameter Parameter Type Description Default value servicename @QueryParam The name of the Group Chat Service conference Examples

Header: Authorization: Basic YWRtaW46MTIzNDU=

Header: Content-Type: application/xml

POST http://example.org:9090/plugins/mucservice/chatrooms

Payload Example 1 (required parameters):

global-1 global Global Chat Room

PHP code will be (tested with MUCservice version 0.2.3 Openfire 3.10.0):

function createRoom($naturalName, $roomName, $description) {
    $url = "http://localhost:9090/plugins/mucservice/chatrooms";
    $data = "<chatRoom>
                <naturalName>$naturalName</naturalName>
                <roomName>$roomName</roomName>
                <description>$description</description>
            </chatRoom>";
    $username = "admin";
    $password = "12345";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PORT, "9090");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
                     array('Content-Type: application/xml',
                           'Authorization: Basic '.base64_encode("$username:$password")));
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    echo "code " . $code;
    print_r($res);
    curl_close($ch);
}

createRoom("room", "room", "room");
like image 188
nguyentran Avatar answered Nov 14 '22 23:11

nguyentran