XMPPHP roster with online status

2019-06-03 15:05发布

问题:

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.

回答1:

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());
}


回答2:

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

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


回答3:

$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


回答4:

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).



标签: php xmpp