问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/16329449/xmpphp-gtalk-status