博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
openfire userserver php
阅读量:7287 次
发布时间:2019-06-30

本文共 2818 字,大约阅读时间需要 9 分钟。

<?php

/**
* PHP Class Openfire User Service Plugin
* see http://www.igniterealtime.org/projects/openfire/plugins/userservice/readme.html
* @copyright 2013 Noercholis
* @license GNU General Public License
* @version Release 1.0.0

* @example See below

* <code>
* <?php
* $user = new UserService;
* // can't enable curl??
* // don't worry, you can use
* // $user = new UserService(false);
*
* $param = array("testuser","testpass","testname","testmail@web.com");
* // sometimes we need to use this parameter structure
* // $param = array(
* // "username"=>"testuser",
* // "password"=>"password",
* // "name"=>"testname",
* // "email"=>"testmail@web.com"
* // );
* echo $result = $user->api("add",$param);
* ?>
* </code>
**/
class UserService{
private $secret="bigsecret";
private $host="localhost";
private $header="http";//or https to avoid sniff,require ssl cert
private $port="9090";
private $plugin = "plugins/userService/userservice";//plugin dir
private $curlEnable=true;
private $cmd=array();
private $base;
public function __construct($curl=true){
$this->curlEnable=$curl;
$this->base=$this->header."://".$this->host;
if(!$curl){
$this->base.=":".$this->port;
}
$this->base.="/".$this->plugin;
$this->cmd=array(
"add"=>array("username","password","name","email"),
"delete"=>array("username"),
"disable"=>array("username"),
"enable"=>array("username"),
"update"=>array("username","password","name","email"),
"add_roster"=>array("username","item_jid","name","subscription"),
"update_roster"=>array("username","item_jid","name","subscription"),
"delete_roster"=>array("username","item_jid")
);
}
public function api($cmd,$param){
if(isset($this->cmd[$cmd])){
$data = $this->buildData($cmd,$param);
}else{
die("Method Not Exists");
}
$content = $this->post($data);
return $content;
}
private function buildData($cmd,$param){
$data = "secret=".$this->secret."&type=".$cmd;
if($this->isAssoc($param)){
$data.="&".http_build_query($param);
}else{
$arr = $this->cmd[$cmd];
for($i=0;$i<count($arr);$i++){
if(isset($param[$i])){
$data.="&".$arr[$i]."=".urlencode($param[$i]);
}
}
}
return $data;
}
private function post($data){
if($this->curlEnable){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->base);
curl_setopt($ch, CURLOPT_PORT,$this->port);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec ($ch);
curl_close ($ch);
}else{
$fopen = fopen($this->base."?".$data, 'r');
$res = fread($fopen, 1024);
fclose($fopen);
}
return $res;
}
private function isAssoc($array){
return array_keys($array) !== range(0, count($array) - 1);
}

}

?>

 

 

使用方法   

$user = new UserService(false);

$param = array("testuser","testpass","testname","");

$result = $user->api("add",$param);

转载于:https://www.cnblogs.com/dcj890828/articles/3613015.html

你可能感兴趣的文章
并发编程四之互斥
查看>>
极速免费-Magento开源产品上传利器magmi
查看>>
magento如何获得产品的属性Minimum Qty Allowed in Shopping Cart
查看>>
FreeMarker循环变量内建函数
查看>>
Python中time模块详解
查看>>
java 的模板方式设计模式
查看>>
跳转到servlet出现java.lang.InstantiationException:
查看>>
RedHat7 配置VNCServer
查看>>
git 回滚版本
查看>>
Nginx反向代理实现会话(session)保持的两种方式
查看>>
Nginx配置指令location匹配符优先级和安全问题
查看>>
sc create 创建启动服务带参数 目录不能有空格
查看>>
Glusterfs初体验
查看>>
Centos搭建SVN服务器三步曲
查看>>
NC-ERP IUFO系统管理要点
查看>>
linux下将文件设置为swap
查看>>
jquery filter()方法
查看>>
make和makefile
查看>>
eclipse git 强制覆盖本地文件
查看>>
elasticsearch查询关键字slop
查看>>