If you are creating any chat application or wanted to send real time messages or data you simplest possible solution is XMPP. Here I'm writing the for sending the messages on XMPP server in PHP. I'm using ejabbered server here. so both sender and receiver have to register themselves over the ejabbered server before sending the messages to each other. Here is the code to register the user on ejabbered XMPP server.
Here is the PHP code to send the messages.
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
|
<?php
class XMPP_SEND_MSG
{
public function sendMSG($form_ejabber_id, $to_ejabber_id) {$command = 'send_message';
$body = array(
'msg' => 'hi',
'cost' => 200);
$body_data = json_encode($body);
$params = array(
'from' => $form_ejabber_id,
'to' => $to_ejabber_id,
'type' => 'chat',
'body' => $body_data,
'subject' => 'any_subject',
);
$this->sendRequest($command, $params);
}
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_SEND_MSG();
$user->sendMSG('from_user@localhost', 'to_user@localhost');
?>
|