给xmpphp添加了几个常用的方法
给xmpphp添加给了以下的常用方法:
registerNewUser //注册一个新用户
addRosterContact //发送添加好友的请求
accept friend request //接受好友请求
deleteRosterContact //删除某个好友
roomMessage //发送群聊消息
createChatRoom //创建群聊
kickUserOutToChatRoom //把某个人剔除群聊
/**
* Register a new user.
*
* @param $entity
* Entity we want information about
*/
public function registerNewUser($user_name, $password = NULL, $email, $name=NULL){ $id = 'reg_' . $this->getID();
$xml = "<iq type='set' id='$id'>
<query xmlns='jabber:iq:register'>
<username>" . $user_name . "</username>
<password>" . $password . "</password>
<email>" . $email . "</email>
<name>" . $name . "</name>
</query>
</iq>";
$this->send($xml); } /**
* Add contact to your roster
*/
public function addRosterContact($jid, $name, $nickName="",$groups=array("Friends")){
// return if there is no jid specified
if(!$jid) return;
// set name to the jid if none is specified
if (!$name) { $name = $jid; }
$id = $this->getID();
$xml = "<iq type='set' id='$id'>";
$xml .= "<query xmlns='jabber:iq:roster'>";
$xml .= "<item jid='$jid' name='$name'>";
foreach ($groups as $group) {
$xml .= "<group>$group</group>";
}
$xml .= "</item>";
$xml .= "</query>";
$xml .= "</iq>"; $xml= <<<EOF
<presence to='{$jid}' type='subscribe'>
<nick xmlns='http://jabber.org/protocol/nick'>{$nickName}</nick>
</presence>
EOF; $this->send($xml);
} /**
* accept friend request
* @param unknown_type $send_jid
* @param unknown_type $received_jid
*/
public function acceptRosterRequest( $send_jid, $receive_jid,$send_name="",$receive_name="" ){ $xml= <<<EOF
<presence from="{$send_jid}" to="{$receive_jid}" type="subscribed">
<nick xmlns='http://jabber.org/protocol/nick'>{$send_name}</nick>
</presence>
<presence from="{$receive_jid}" to="{$send_jid}" type="subscribed">
<nick xmlns='http://jabber.org/protocol/nick'>{$receive_name}</nick>
</presence>
EOF;
$this->send($xml); } /**
* Contact you wish to remove
* @param $jid
*
*/
public function deleteRosterContact($jid) {
$id = $this->getID();
$xml = "<iq type='set' id='$id'>";
$xml .= "<query xmlns='jabber:iq:roster'>";
$xml .= "<item jid='" . $jid . "' subscription='remove' />";
$xml .= "</query>";
$xml .= "</iq>";
$this->send($xml);
} /**
* send group message
* @param unknown_type $to
* @param unknown_type $body
* @param unknown_type $type
* @param unknown_type $subject
* @param unknown_type $payload
*/
public function roomMessage($jid,$room_jid, $body, $subject = null, $payload = null, $user_name=null) { if( $user_name ){
// $present_roomId=$room_jid."/".$user_name;
$present_roomId=$room_jid."/".$user_name."_"; //加“_”是因为防止同一个帐号在两个地方用同样的nickname登录房间,会有一个地方会退出登录的
}
else{
$present_roomId=$room_jid;
} $id=$this->getID(); $out= <<<EOF
<presence
from='{$jid}'
to='{$present_roomId}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence> EOF; $jid = htmlspecialchars($jid);
$body = htmlspecialchars($body);
$subject = htmlspecialchars($subject); $out .= "<message from=\"{$jid}\" to=\"{$room_jid}\" type='groupchat'>";
if($subject) $out .= "<subject>$subject</subject>";
$out .= "<body>$body</body>";
if($payload) $out .= $payload;
$out .= "</message>"; $this->send($out); } /**
* create chat group
* @param unknown_type $jid, creater's jid
* @param unknown_type $room_jid chatroom's jid
*
* take example
* in a class, how to use this methodd
*
//modify room default setting
$room_setting=array('muc#roomconfig_roomname'=>$testName,'muc#roomconfig_roomdesc'=>$testDesc,'muc#roomconfig_changesubject'=>1); //create chatroom , save chatroom settting
$this->_conn->createChatRoom($jid,$test_id,$real_name,$room_setting,$this); //set chatroom setting to xmpp server, in this class, need a public $room_xml
$this->_conn->sendChatroom_setting($jid,$test_id,$real_name,$this->room_xml);
*
*/
public function createChatRoom($jid, $room_jid,$real_name,$room_setting=array(),$ref_obj){ $this->refObj=$ref_obj;
$this->room_setting=$room_setting;
$id=$this->getID(); $this->from=$jid;
$this->to=$room_jid; $xml= <<<EOF
<presence
from='{$jid}'
to='{$room_jid}/{$real_name}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
<iq from='{$jid}'
id="{$id}"
to='{$room_jid}'
type='get'>
<query xmlns='http://jabber.org/protocol/muc#owner'/>
</iq>
EOF; $this->addIdHandler($id, 'setChatroom');
$this->send($xml);
} /**
* set chatroom setting
* @param unknown_type $xml
*/
public function setChatroom($xml){ $xml->attrs['type']="set";
$xml->attrs['from']=$this->from;
$xml->attrs['to']=$this->to;
$xml->subs[0]->subs[0]->attrs['type']='submit';
foreach( $xml->subs[0]->subs[0]->subs as &$node ){
if( isset($node->attrs['var']) && isset( $this->room_setting[$node->attrs['var']] ) ){
$node->subs[0]->data=$this->room_setting[$node->attrs['var']];
}
} $this->refObj->room_xml=$xml->toString();
} /**
* please call setChatroom() before call this method
*/
public function sendChatroomSetting($jid, $room_jid,$real_name,$room_xml){ $xml= <<<EOF
<presence
from='{$jid}'
to='{$room_jid}/{$real_name}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
EOF; $xml.=$room_xml;
$this->send($xml); } /**
* kick user out to chat room
* @param unknown_type $jid, create's jid
* @param unknown_type $room_jid,chatroom's jid
* @param unknown_type $real_name,kickout user's jid
* @param unknown_type $room_jid,chatroom's jid
*/
public function kickUserOutToChatRoom($jid, $room_jid,$real_name,$kick_names=array()){ $xml= <<<EOF
<presence
from='{$jid}'
to='{$room_jid}/{$real_name}'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
EOF; foreach( $kick_names as $kick_name ){ $id=$this->getID(); $xml.= <<<EOF
<iq from='{$jid}'
id='{$id}'
to='{$room_jid}'
type='set'>
<query xmlns='http://jabber.org/protocol/muc#admin'>
<item nick='{$kick_name}' role='none'>
<reason>manger kick you out of room</reason>
</item>
</query>
</iq>
EOF; } $this->send($xml);
} /**
*
* @param XML Object $xml
*/
protected function delete_roster_contact_handler($xml) {
// do any handling you wish here
$this->event('contact_removed');
} public function getJid(){
return $this->jid;
}
代码已开源,地址:
https://github.com/newjueqi/xmpp
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2014.06.25更新
关于创建群组的例子,请看XMPPHP/test.php 文件中的 createEventChatRoom()
[文章作者]曾健生
[作者邮箱]h6k65@126.com
[作者QQ]190678908
[新浪微博] @newjueqi
[博客] http://blog.csdn.net/newjueqi
http://blog.sina.com.cn/h6k65
版权声明:本文为博主原创文章,未经博主允许不得转载。
给xmpphp添加了几个常用的方法的更多相关文章
- WebAPi添加常用扩展方法及思维发散
前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...
- iOS常用公共方法
iOS常用公共方法 字数2917 阅读3070 评论45 喜欢236 1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat si ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- Delphi中TStringList类常用属性方法详解
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...
- (转)Android之常用功能方法大集合
这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...
- php中的常用魔术方法总结
以下是对php中的常用魔术方法进行了详细的总结介绍,需要的朋友可以过来参考下 常用的魔术方法有:__Tostring () __Call() __autoLoad() __ clone() __GET ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- js中关于string的一些常用的方法
最近总结了一些关于string中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. ...
- python字符串常用的方法解析
这是本人在学习python过程中总结的一些关于字符串的常用的方法. 文中引用了python3.5版本内置的帮助文档,大致进行翻译,并添加了几个小实验. isalnum S.isalnum() -> ...
随机推荐
- 一个简单的基于 DirectShow 的播放器 1(封装类)
DirectShow最主要的功能就是播放视频,在这里介绍一个简单的基于DirectShow的播放器的例子,是用MFC做的,今后有机会可以基于该播放器开发更复杂的播放器软件. 注:该例子取自于<D ...
- 安卓TV开发(六) 移动智能终端UI之实现类似GridView的焦点控制FocusView框架
转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40045089,作者:skay 前言 安卓TV开发(五) 移动智能终端UI之实现主流 ...
- 8 个实用的 Bootstrap 3 案例教程
Bootstrap 3发布各大设计论坛议论纷纷.这次Bootstrap 3最大的特点就是--扁平化.下面就是一些早期的Bootstrap 3例子,不过亲们注意咯,因为大部分最早期的测试案例,可能用到一 ...
- 云技术:弹性计算ECS
云计算(Cloud Computing)被业界看作继大型计算机.个人计算机.互联网之后的第四次IT产业革命,正日益成为未来互联网与移动技术相结合的一种新兴计算模式.云计算提供了IT基础设施和平台服务的 ...
- 《转》iOS 平台 Cocos2d-x 项目接入新浪微博 SDK 的坑
最近在做一个 iOS 的 cocos2d-x 项目接入新浪微博 SDK 的时候被“坑”了,最后终于顺利的解决了.发现网上也有不少人遇到一样的问题,但是能找到的数量有限的解决办法写得都不详细,很难让人理 ...
- angularjs指令中的compile与link函数详解
这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下 通常大家在 ...
- JVM如何理解Java泛型类
//泛型代码 public class Pair<T>{ private T first=null; private T second=null; public Pair(T fir,T ...
- sublime使用package control安装插件
sublime本身可以集成Package Control来进行插件安装,非常方便. 1. 安装package control(插件包管理) 1.1 进入https://packagecontrol.i ...
- YUV420格式解析
一般的的YUV420图像格式实际上是Y'UV,420指的是其在Y U V上面的采样率.在YUV420的格式中,首先存储每一个像素的Y'值,然后跟着存储的是每2*2方阵采样一次的U值,最后存储的是每2* ...
- Wooden Sticks -HZNU寒假集训
Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...