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, 'xxxx@gmail.com', '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.

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
ialphan
  • 1,241
  • 1
  • 17
  • 30
  • It's been a while since I integrated into XMPP. Can you increase the logging level and show us the raw response from google? – Kevin May 07 '13 at 18:45
  • 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. – pregmatch May 11 '13 at 12:15

2 Answers2

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, 'xxxx@gmail.com', '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 = 'user@domain.tld'; // 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.

lwitzel
  • 591
  • 5
  • 15
  • 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: https://github.com/ialphan/IMStatus – ialphan Feb 21 '14 at 21:12
  • Hi i have added something like $conn->processUntil('session_start'); $conn->presence(); $my_jid = 'test-adipso03@xxx.211.99.162'; // 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! – Shreejibawa Jun 24 '14 at 11:42
  • 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. – lwitzel Jun 25 '14 at 17:03
-1

You should be able to request your own presence by passing your own jid (username@gmail.com) 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

Kevin
  • 523
  • 4
  • 20
  • Why not? Is your roster empty? Any output? Errors? No feedback.. I can't help. – Kevin May 10 '13 at 15:02
  • 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? – ialphan May 10 '13 at 15:49
  • 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 " – Kevin May 10 '13 at 16:03