XMPPHP 名单及在线状态

发布于 2024-08-02 02:30:42 字数 186 浏览 2 评论 0原文

我正在使用 XMPPHP 检索我的应用程序用户 GMail 帐户的名册。
XMPPHP 还能告诉我花名册联系人的在线状态吗?
我似乎无法找到如何做到这一点...

干杯。

I'm using XMPPHP to retrieve the roster of my application users GMail account.
Can XMPPHP also tell me the roster contacts online status?
I can't seem to find how to do that...

cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离去的眼神 2024-08-09 02:30:43

以下是花名册列表和 GMail 用户在线状态的示例;

$user_name = 'ENTER_EMAIL_ID';
$password = 'ENTER_PASSWORD';
$end_loop = 0;

$conn = new XMPPHP_XMPP('talk.google.com', 5222, $user_name,$password, "xmpphp", 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
$conn->autoSubscribe(); 

try {
    $conn->connect();   
    while($end_loop <=0) {
        $payloads = $conn->processUntil(array('end_stream', 'session_start','roster_received'));
        foreach($payloads as $event) {
            $pl = $event[1];
            switch($event[0]) {

                case 'session_start':
                    $conn->getRoster();
                    $conn->presence('I m presence'.time());
                break;

                case 'roster_received':
                $array_contact=$pl;

                foreach($array_contact as $user => $friends_name){
                    echo "<li>".$user.'_NAME_'.$friends_name['name'].'</li>';
                }
                $end_loop++;
                break;
            }
        }       
    }

    while(1)
    {
        $payloads = $conn->processUntil(array('presence'));
        echo "<li>".$payloads[0][1]['from']."_Show_". $payloads[0][1]['show']."</li>";

        $_SESSION[$payloads[0][1]['from']] = "~~";
    }

$conn->disconnect();

} catch(XMPPHP_Exception $e) {
    die($e->getMessage());
}

Here is an example of roster list and the online presence of GMail users;

$user_name = 'ENTER_EMAIL_ID';
$password = 'ENTER_PASSWORD';
$end_loop = 0;

$conn = new XMPPHP_XMPP('talk.google.com', 5222, $user_name,$password, "xmpphp", 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
$conn->autoSubscribe(); 

try {
    $conn->connect();   
    while($end_loop <=0) {
        $payloads = $conn->processUntil(array('end_stream', 'session_start','roster_received'));
        foreach($payloads as $event) {
            $pl = $event[1];
            switch($event[0]) {

                case 'session_start':
                    $conn->getRoster();
                    $conn->presence('I m presence'.time());
                break;

                case 'roster_received':
                $array_contact=$pl;

                foreach($array_contact as $user => $friends_name){
                    echo "<li>".$user.'_NAME_'.$friends_name['name'].'</li>';
                }
                $end_loop++;
                break;
            }
        }       
    }

    while(1)
    {
        $payloads = $conn->processUntil(array('presence'));
        echo "<li>".$payloads[0][1]['from']."_Show_". $payloads[0][1]['show']."</li>";

        $_SESSION[$payloads[0][1]['from']] = "~~";
    }

$conn->disconnect();

} catch(XMPPHP_Exception $e) {
    die($e->getMessage());
}
春夜浅 2024-08-09 02:30:43

我还没有尝试过使用 Google Talk,但通常您正在寻找

$roster->getPresence($jid)['status']

I haven't tried it with Google Talk, but generally you're looking for

$roster->getPresence($jid)['status']
深海不蓝 2024-08-09 02:30:43
$uStatus = $conn->roster->getPresence($jid);

echo "Online status: " . $uStatus['show']; // tells whether available or unavailable or dnd
echo "Status message: " . $uStatus['status']; // shows the user's status message
$uStatus = $conn->roster->getPresence($jid);

echo "Online status: " . $uStatus['show']; // tells whether available or unavailable or dnd
echo "Status message: " . $uStatus['status']; // shows the user's status message
七七 2024-08-09 02:30:43

我在这里发布了类似问题的答案: XMPPHP GTalk Status

以下是让它工作的关键:

  1. $conn->presence() 不仅将您的状态发送到服务器,还收集每个联系人的状态并填充您的名册。 它实际上与 $conn->getRoster() 执行相同的操作,但也收集每个联系人的状态信息。
  2. 您必须延迟脚本,以便让服务器有机会发送 iq 块,这些块似乎一次只有一个联系人。 我在示例代码中看到了 $conn->processUntil('presence') 和 $conn->processUntil('roster_received') ,但第一个等待的时间不够长,第二个永远不会结束。 我最终使用 $conn->processTime(2) 强制它等待 2 秒以确保它获得所有 iq 块。

对我来说另一个关键是打开详细日志记录。 您可以在初始对象构造中执行此操作:

$conn = new XMPPHP_XMPP('talk.google.com', 5222, $user_name,$password, "xmpphp", 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);

这将向您的输出(在我的例子中为浏览器窗口)输出详细日志。

I posted an answer to a similar question here: XMPPHP GTalk Status

Here are the keys to getting it to work:

  1. $conn->presence() not only sends your status to the server, but also collects the status of each of your contacts and populates your roster. It actually does the same thing as $conn->getRoster(), but also collects presence info for each contact.
  2. You've got to delay the script in order to give the server a chance to send over the iq blocks, which appear to come one contact at a time. I've seen both $conn->processUntil('presence') and $conn->processUntil('roster_received') used in sample code, but the first one doesn't wait long enough, and the 2nd one never ends. I ended up using $conn->processTime(2) to force it to wait 2 seconds to make sure it got all the iq blocks.

The other key for me was turning on verbose logging. You do that in your initial object construction:

$conn = new XMPPHP_XMPP('talk.google.com', 5222, $user_name,$password, "xmpphp", 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);

This will output a verbose log to whatever your output is (browser window in my case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文