Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMPPHP GTalk Status

I’m trying to get my online status using XMPPHP and I can’t seem to get anything that has my status from the $conn. Here is a snippet of my code:

require_once('XMPPHP/XMPP.php');

$conn = new XMPPHP_XMPP('talk.google.com', 5222, '[email protected]', 'xxxxx', 'xmpphp', 'gmail.com', $printlog = false, $loglevel = XMPPHP_Log::LEVEL_INFO);

$conn->connect();
$conn->processUntil('session_start');
$conn->presence($status='Controller available.');
var_dump($conn); // this gives me a long output but nothing about status. ex: http://pastebin.com/yfs1V5Jb

I also tried getRoster() to see a list of my friend’s info (although I’m only interested in mine) but no luck.

Any suggestions how I can get this to work? Thanks.

like image 443
ialphan Avatar asked May 02 '13 02:05

ialphan


1 Answers

I've been grappling with this issue for the last 2 days, and finally figured out a hack to get things to work. I'm documenting it here, because this was the stack overflow question that appeared most often for me while searching for answers.

The $conn->presence() method not only sends your presence info to the server; it also collects presence info for every contact from the server. The fundamental problem is that when you send the $conn->presence() command, you have to give the script time to receive and process this information from the server. The example scripts all use $conn->processUntil('presence') to do this, but for some reason for me that didn't pause things long enough to get all the roster information.

To get around this, I finally just used $conn->processTime(2), forcing things to wait 2 seconds before proceeding. This is good enough for my purposes, but is clearly a hack. So using your code as an example:

require_once('XMPPHP/XMPP.php');

$conn = new XMPPHP_XMPP('talk.google.com', 5222, '[email protected]', 'xxxxx', 'xmpphp', 'gmail.com', $printlog = true, $loglevel = XMPPHP_Log::LEVEL_VERBOSE);

$conn->connect();
$conn->processUntil('session_start');
$conn->presence($status='Controller available.');
$conn->processTime(2);

// now see the results
$roster = $conn->roster->getRoster();
print_r($roster); // you should now see roster array with presence info for each contact

To answer your question more specifically, you could use the following in place of the code under "now see the results":

$my_jid = '[email protected]'; // put your jid here
$status = $conn->roster->getPresence($my_jid);
echo $status['show'];

That will display the online status for the jid you provide.

Note that in this example I also changed the constructor to display the most verbose log possible. This was key to helping me work through this.

A better solution would obviously be to add a $conn->processUntil('roster') command to the framework, or something like that. But since the framework hasn't been updated in 5 years, that's unlikely to happen.

Hopefully this will save someone the hours I lost trying to solve it. Cheers.

like image 175
lwitzel Avatar answered Oct 25 '22 15:10

lwitzel