XMPPHP GTalk Status
Asked Answered
M

2

16

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.

Myrt answered 2/5, 2013 at 2:33 Comment(2)
It's been a while since I integrated into XMPP. Can you increase the logging level and show us the raw response from google?Epicontinental
How to you mean status? Status message or status Away/Online etc? If you need status message then $conn->presence($status='Controller available.'); is your status.Outer
L
5

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.

Lujan answered 10/1, 2014 at 23:19 Comment(3)
Get the instant messenger status(online, offline) of the specified user for a specific service in PHP. Supported services are AIM, Facebook*, GTalk, ICQ, Skype and YAHOO: github.com/ialphan/IMStatusMyrt
Hi i have added something like $conn->processUntil('session_start'); $conn->presence(); $my_jid = '[email protected]'; // put your jid here $status = $conn->roster->getPresence($my_jid); echo $status['show']; Doesn't seem to work for me. I am unable to get user's status. It shows blank!Pensive
jayshahagile, did you open the connection with the most verbose log possible? The log should point you to the issue. If you want more help, start a new question and message me.Lujan
E
-1

You should be able to request your own presence by passing your own jid ([email protected]) to getPresence();

For example:

$status = $conn->roster->getPresence($jid);
var_dump($status);    // Make sure you are retrieving a populated presence array
echo $status['show']; // available,unavailable,dnd
echo $status['status']; //status message

Quite a while back I ran into an issue with this library not populating roster records. If you run into this issue, you should apply the patch detailed here: https://code.google.com/p/xmpphp/issues/detail?id=44&q=empty

Epicontinental answered 7/5, 2013 at 21:9 Comment(3)
Why not? Is your roster empty? Any output? Errors? No feedback.. I can't help.Epicontinental
You are right I should have been more descriptive in my comment. There were no errors, just the returned "NULL". Do you have a working test page?Myrt
Unfortunately I cannot disclose my working page due to an NDA. It's likely due to the bug I mentioned above. You cannot query for the presence of users that are not in your roster. To test if you are a victim of this bug you'll find $conn->getRoster() does not populate the roster array, but it returns the correct XML response (turn on debugging you should see "<iq .. type='result'><queryxmlns='jabber:iq:roster'><item .."). The patch might give you a warning but it's easy to fix.Epicontinental

© 2022 - 2024 — McMap. All rights reserved.