我需要一个用于PHP的XMPP库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我需要一个用于PHP的XMPP库相关的知识,希望对你有一定的参考价值。

而且我不能使用作曲器(服务器在防火墙后面,php-download.com无法正常工作]

特别是,我需要导出用户列表及其组

我已经尝试过https://code.google.com/archive/p/xmpphp/,但它与PHP 7不兼容

答案

因此您不能在服务器上使用composer,但是为什么不在计算机上使用composer会加载相关性并上传供应商文件夹。然后,您便拥有了所需的一切。

Composer将所有依赖项下载到vendor文件夹,并生成一些自动加载文件。当您上传孔项目时,它应该可以工作。

另一答案
$server   = "SERVER NAME HERE";
$username = "USER NAME HERE";
$password = "PASSWORD HERE";
$resource = "globe";
$streamid = "";

function vardump($data, $title = false){
    if($title){
        echo '<H2>' . $title . '</H2>';
    }
    echo '<PRE>';
    var_dump($data);
    echo '</PRE>';
}

function open_connection($server) {
    $connection = fsockopen($server, 5222, $errno, $errstr);
    if (!$connection) {
        print "$errstr ($errno)<br>";
        return false;
    }
    return $connection;
}

function send_xml($connection, $xml) {
    fwrite($connection, $xml);
}

function textcontains($text, $searchfor, $or = false){
    return stripos($text, $searchfor) !== false;
}

function send_recv_xml($connection, $xml, $size = 4096) {
    send_xml($connection, $xml);
    $data = recv_xml($connection, $size);
    $data["sent_xml"] = $xml;
    $data["sent_html"] = htmlspecialchars($xml);
    return $data;
}

function fread_untildone($connection, $size = 4096){
    $content = '';
    if($size < 0){
        while(!textcontains($content, '</iq>')){
            $content .= fread($connection, abs($size));
        }
    } else {
        $content = fread($connection, $size);
    }
    /*while(!feof($connection)){//did not work
        $content .= fread($connection, $size);
    }*/
    return $content;
}

function recv_xml($connection, $size = 4096) {
    $xml = fread_untildone($connection, $size);
    if ($xml === "") {
         return null;
    }
    // parses xml
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $xml, $val, $index);
    xml_parser_free($xml_parser);
    $RET = array($val, $index);
    $RET["originaldata"] = $xml;
    $RET["specialdata"] = htmlspecialchars($xml);
    $RET["time"] = time();
    return $RET;
}

function find_xmpp($connection, $tag, $value=null, &$ret=null) {
    static $val = null, $index = null;
    do {
        if ($val === null && $index === null) {
            list($val, $index) = recv_xml($connection);
            if ($val === null || $index === null) {
                return false;
            }
        }
        foreach ($index as $tag_key => $tag_array) {
            if ($tag_key === $tag) {
                if ($value === null) {
                    if (isset($val[$tag_array[0]]['value'])) {
                        $ret = $val[$tag_array[0]]['value'];
                    }
                    return true;
                }
                foreach ($tag_array as $i => $pos) {
                    if ($val[$pos]['tag'] === $tag && isset($val[$pos]['value']) &&
                        $val[$pos]['value'] === $value) {
                            $ret = $val[$pos]['value'];
                            return true;
                    }
                }
            }
        }
        $val = $index = null;
    } while (!feof($connection));

    return false;
}

function xmpp_connect($server, $username, $password, $resource = "globe") {
    global $streamid;
    $connection = open_connection($server);
    if (!$connection) {
        return false;
    }
    send_xml($connection, '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" xmlns="jabber:client" to="' . $server . '" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">');
    $data = recv_xml($connection);
    $streamid = $data[0][0]["attributes"]["ID"];
    //vardump($streamid, "stream id:");

    send_xml($connection, '<iq type="get" to="' . $server . '" id="auth1"><query xmlns="jabber:iq:auth"><username>' . $username . '</username></query></iq>');
    $data = recv_xml($connection);

    $XML = '<iq type="set" id="auth2"><query xmlns="jabber:iq:auth">';
    $fields = [];
    foreach($data[1] as $KEY => $VALUE){
        $KEY = strtolower($KEY);
        switch($KEY){
            case "username":    $VALUE = $username; break;
            case "password":    $VALUE = $password; break;
            case "digest":      $VALUE = strtolower(sha1($streamid . $password)); break;
            case "resource":    $VALUE = $resource; break;
            default: $VALUE = "";
        }
        $fields[ $KEY ] = $VALUE;
        if($VALUE){
            $XML .= '<' . $KEY . '>' . $VALUE . '</' . $KEY . '>';
        }
    }
    $XML .= '</query></iq>';
    //vardump($fields, "auth");

    $data = send_recv_xml($connection, $XML);
    //vardump($data, 'login');

    return $connection;
}

function xmpp_enum_users($connection, $server, $username, $resource = "globe"){
    //$XML = '<iq from="' . $username . '@' . $server . '/' . $resource . '" id="get-registered-users-list-1" to="' . $server . '" type="set" xml:lang="en">';
    //$XML .= '<command xmlns="http://jabber.org/protocol/commands" action="execute" node="http://jabber.org/protocol/admin#get-registered-users-list"/></iq>';

    /*$XML = '<iq from="' . $username . '" type="result" to="' . $username . '@' . $server . '/' .  $resource . '" id="123">
    <query xmlns="http://jabber.org/protocol/disco#items">
        <item jid="conference.localhost" />
        <item jid="pubsub.localhost" />
        <item jid="riot.localhost" />
        <item jid="vjud.localhost" />
        <item node="announce" name="Announcements" jid="localhost" />
        <item node="config" name="Configuration" jid="localhost" />
        <item node="user" name="User Management" jid="localhost" />
        <item node="online users" name="Online Users" jid="localhost" />
        <item node="all users" name="All Users" jid="localhost" />
        <item node="outgoing s2s" name="Outgoing s2s Connections" jid="localhost" />
        <item node="running nodes" name="Running Nodes" jid="localhost" />
        <item node="stopped nodes" name="Stopped Nodes" jid="localhost" />
    </query></iq>';*/
    global $streamid;
    $XML = '<iq from="' . $username . '@' . $server . '/' .  $resource . '" id="' . $streamid . '" type="get"><query xmlns="jabber:iq:roster"/></iq>';
    return send_recv_xml($connection, $XML, -4096);
}

function xmpp_disconnect($connection){
    fclose($connection);
}


$connection = xmpp_connect($server, $username, $password, $resource);
$data = xmpp_enum_users($connection, $server, $username, $resource);
$xml = simplexml_load_string($data["originaldata"]);
$users = [];
foreach($xml->query->item as $user){
    $username = $user->attributes()->name;
    $group = $user->group[0];
    $users[$username] = $group;
}

vardump($users, "users");
echo '<HR>' . json_encode($users);
echo '<HR>';
foreach($users as $username => $group){
    echo $username . '=' . $group . '<BR>';
}

xmpp_disconnect($connection);

这是XMPP客户端的开始。花了一段时间才找到要连接的基本代码,我注意到其他人要求这样做但没有得到它。

以上是关于我需要一个用于PHP的XMPP库的主要内容,如果未能解决你的问题,请参考以下文章

用于 React Native Android 的 XMPP 库

XMPP——openfire、PHP 和 python 网络服务

如何使用 JAXL PHP 库 IQ Query Action Protocol XMPP

qt5中用于移动跨平台编程的xmpp客户端库

PHP 中的 XMPP + OTR 加密

xmpp openfire 未验证我稍后用于 strophe 附加的 php 脚本