我已经用xmpphp库连接了ejabberd xmpp服务器。它工作得很好。我需要使用xmpphp库在ejabberd xmpp服务器中创建一个用户。因此,我在XMPPHP/XMPP.php文件中添加了以下两个函数:
public function register($username, $password = null){
if (!isset($password)) $password = $this->genRandomString(15);
$id = 'reg_' . $this->getID();
$xml = "<iq type='set' id='$id'>
<query xmlns='jabber:iq:register'>
<username>" . $username . "</username>
<password>" . $password . "</password>
<email></email>
<name></name>
</query>
</iq>";
$this->addIdHandler($id, 'register_new_user_handler');
$this->send($xml);
}
protected function register_new_user_handler($xml){
switch ($xml->attrs['type']) {
case 'error':
$this->event('new_user_registered', 'error');
break;
case 'result':
$query = $xml->sub('query');
$username='';
$password='';
if(!is_array($query->subs)) {
foreach ($query->sub as $key => $value) {
switch ($value->name) {
case 'username':
$username = $value->data;
break;
case 'password':
$password = $value->data;
break;
}
}
}
$this->event('new_user_registered', array('jid' => $username . "@{$this->server}", 'password' => $password));
default:
$this->event('new_user_registered', 'default');
}
}并在sendmessage_example.php中调用上述函数,如下所示:
<?php
// activate full error reporting
//error_reporting(E_ALL & E_STRICT);
include 'XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP('serverhost', 5222, 'admin@localhost', 'password', 'xmpphp', 'localhost', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
try {
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->register('uname', 'pass');
$conn->message('vaiju@localhost', 'This is a test message!');
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}我手动添加了一个名为vaiju的用户。我能够连接它,并且在我的pidgin客户端中收到一条消息。但是用户注册不能正常工作。
发布于 2015-01-13 17:20:24
你可以这样做。经过测试并和我一起工作。
define('JABBER_REST_HOST','localhost:5285');
define('JABBER_REST_URL','http://localhost:5285/rest');
$request = "register ketan13 localhost ketan13";
$jabberResponse = sendRESTRequest(JABBER_REST_URL, $request);
echo '<pre>'; print_r($jabberResponse);
function sendRESTRequest ($url, $request) {
// Create a stream context so that we can POST the REST request to $url
$context = stream_context_create (array ('http' => array ('method' => 'POST'
,'header' => "Host: ".JABBER_REST_HOST."\nContent-Type: text/html; charset=utf-8\nContent-Length: ".strlen($request)
,'content' => $request)));
// Use file_get_contents for PHP 5+ otherwise use fopen, fread, fclose
if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
$result = file_get_contents($url, false, $context);
} else {
// This is the PHP4 workaround which is slightly less elegant
// Suppress fopen warnings, otherwise they interfere with the page headers
$fp = @fopen($url, 'r', false, $context);
$result = '';
// Only proceed if we have a file handle, otherwise we enter an infinite loop
if ($fp) {
while(!feof($fp)) {
$result .= fread($fp, 4096);
}
fclose($fp);
}
}
return $result;
}https://stackoverflow.com/questions/27918007
复制相似问题