使用composer形式安装的JAXL
<?php
require_once "vendor/autoload.php";
$client = new JAXL(array(
'jid' => '',
'pass' => '',
'host'=>'ubuntu',
'auth_type'=>'ANONYMOUS', //匿名登录
'log_level' => JAXLLogger::DEBUG,
));
$_room_full_jid = "test@conference.ubuntu/ordersender";
$room_full_jid = new XMPPJid($_room_full_jid);
$client->require_xep(array(
'0045', // MUC
'0203', // Delayed Delivery
'0199', // XMPP Ping
));
$client->add_cb('on_auth_success', function() {
global $client, $room_full_jid;
// set status
$client->set_status("available!",'dnd',10);
$client->get_vcard(); // fetch your vcard
$client->get_roster(); // fetch your roster list
JAXLLogger::info("got on_auth_success cb, jid ".$client->full_jid->to_string());
JAXLLogger::info("got on_auth_success cb, room_full_jid ".$room_full_jid->to_string());
// join muc room
$client->xeps['0045']->join_room($room_full_jid);
//start send message
//@link http://wiki.jabbercn.org/XEP-0045#.E5.8F.91.E9.80.81.E6.B6.88.E6.81.AF.E7.BB.99.E6.89.80.E6.9C.89.E6.88.BF.E5.AE.A2 例子60的xml格式
//@link JAXL 文档 https://jaxl.readthedocs.io/en/latest/users/xml_objects.html
$xml = new JAXLXml('message');
$_room_full_jid2 = "test@conference.ubuntu";
$to2 = new XMPPJid($_room_full_jid2);
$xml->attrs(array(
'from'=>$client->full_jid->to_string(),
'to'=>$to2->to_string(),
'type'=>'groupchat'
));
$xml->c('body')->t('Hello world');
//xml<message from="244a387a@ubuntu/244a387a" to="test@conference.ubuntu" type="groupchat"><body>Hello world</body></message>
JAXLLogger::info("xml".$xml->to_string());
$client->send($xml); //发送消息
$client->send_end_stream(); //断开连接
});
function on_auth_failure_callback($reason)
{
global $client;
$client->send_end_stream();
JAXLLogger::info("got on_auth_failure cb with reason $reason");
}
$client->add_cb('on_auth_failure', 'on_auth_failure_callback');
function on_disconnect_callback()
{
JAXLLogger::info("got on_disconnect cb");
}
$client->add_cb('on_disconnect', 'on_disconnect_callback');
$client->start();