Get user presence via jQuery/PHP

Hi all,

I’m trying to retrieve the user (not in roster) presence via jQuery or PHP to dosplay in a webpage which user are online or not.

If I try to retrieve it by jQuery i get this error from Firebug:

GET http://saturno.ed:9090/plugins/presence/status?jid=##@domain&type=xml&_=1329907925328 200 OK 84ms

Ajax code is this:

$.ajax({
            url: 'http://server.domain:9090/plugins/presence/status?jid=##@domain&type=xml',
            async: false,
            cache: false,
            dataType: "text/xml",
            success: function(data){                 console.log('data: '+data);
            }, //end success
            error:function(xhr,err){
                console.log("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
                console.log("responseText: "+xhr.responseText);
            }
    });//end ajax

From console.log:

data: readyState: 4status: 0 responseText:

If i try to get presence from PHP with this code

$url = "http://server.domain:9090/plugins/presence/status?jid=##@domain&type=xml";
$str = file_get_contents($url);

i get this:

<presence type="error"><error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>

Where i wrong? How i can get via Ajax user’s presence?

Tkx to all!

To get presence via PHP you would need to use cURL:

$presence_string =

$ch = curl_init($presence_string);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

if you want to use AJAX, I would simply make an AJAX call to a PHP file with the above call, and JSON encode the result before you return it.

However, based on your last bit of info, it looks like you aren’t using the req_jid field, as in the presence service config you have it set that only ‘Subscribed’ users can get the info. Change it to ‘Anyone’ and see if your result changes.

tkx for your answer. I never used cURL and i don’t undersand where, in string you passed me, i can put user jid to know his presence.

Can you help me?

What do you get back if you just post the URL into your browser?

I use this php code:

$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://server.domain:9090/plugins/presence/status?jid=##@domain&type=xml");
  curl_setopt($ch, CURLOPT_HEADER, 0);
  $str = curl_exec($ch);
  curl_close($ch);

and i get this error:

<presence type="error"><error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>

Presence visibility plugin is set to Anyone.