Here we have created the chat room in XMPP using PHP and Here we have defined how to join a chant room in XMPP.
Now I'm going to explain how to send a message into a chat room in XMPP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?php class XMPP_MSG_TO_ROOM { public function sendMsg($room_name) { $command = 'send_message'; $body = array( 'msg' => 'hi', 'cost' => 200); $body_data = json_encode($body); $params = array( 'from' => $from_jabber_id, 'to' => $room_name . '@conference.localhost', 'type' => 'groupchat', 'body' => $body_data, 'subject' => 'any_subject', ); $response = $this->sendRequest($command, $params); return $response; } public function sendRequest($command, $params) {$request = xmlrpc_encode_request($command, $params, (array('encoding' => 'utf-8'))); $context = stream_context_create(array('http' => array( 'method' => "POST", 'header' => "User-Agent: XMLRPC::Client mod_xmlrpc\r\n" . "Content-Type: text/xml\r\n" . "Content-Length: " . strlen($request), 'content' => $request ))); // edit RPC server url $RPC_SERVER = 'http://IP:4560/RPC2' $file = file_get_contents($RPC_SERVER , false, $context); $response = xmlrpc_decode($file); if (xmlrpc_is_fault($response)) { trigger_error("xmlrpc: $response[faultString] ($response[faultCode])"); } else { return $response; } } } $user = new XMPP_MSG_TO_ROOM(); $user->sendMsg('room_name'); ?> |