2015-01-13 2 views
0

xmpphp 라이브러리로 ejabberd xmpp 서버를 연결했습니다. 잘 작동합니다. xmpphp 라이브러리를 사용하여 ejabberd xmpp 서버에서 사용자를 생성해야합니다. 그래서 내가 XMPPHP/XMPP.php 파일에 두 가지 기능을 다음과 같은 추가 :xmpphp로 ejabberd에 사용자 등록

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 위의 함수를 호출 M :

<?php 

// activate full error reporting 
//error_reporting(E_ALL & E_STRICT); 

include 'XMPPHP/XMPP.php'; 


$conn = new XMPPHP_XMPP('serverhost', 5222, '[email protected]', '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('[email protected]', 'This is a test message!'); 
    $conn->disconnect(); 
} catch(XMPPHP_Exception $e) { 
    die($e->getMessage()); 
} 

내가 수동라는 사용자를 추가 한 바이 주. 나는 그것을 연결하고 내 피진 클라이언트에서 메시지를 받고 있어요. 그러나 사용자 등록이 제대로 작동하지 않습니다.

누군가 나를 도와주세요 ... 감사합니다.

+0

jabber 서버의 iq 요청에 대한 응답은 무엇입니까? –

+0

응답 메시지가 표시되지 않지만 테스트 메시지입니다. 사용자는 ejabberd 서버에서 생성하지 않습니다. – Sans

답변

0

이렇게 할 수 있습니다. 테스트를 거쳐 나와 협력하고 있습니다.

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; 
} 
+0

아니, 그게 나를 위해 작동하지 않습니다. 위 코드를 설명 할 수 있습니까? 그래서 나는 그것을 가능하게 만들 수있다. – Sans

+0

URL을 적절하게 변경하셨습니까? 포트 5285가 열려 있습니까? 또한 작동하지 않는 경우 오류 코드를 보냅니다. –

+0

여전히 작동하지 않습니다. 오류 코드가 없습니다. – Sans