Can't get list of users (roster) from xmpphp - php

I have an app where I send xmpp message to some devices. This successfully works. But now I want to recieve roster (list of connected users) and I get empty array, however there are 4 users there. Here is my code
require_once($_SERVER["DOCUMENT_ROOT"]."/lib/xmpphp/XMPP.php");
$con=$conf->getXMPPObj();
try {
$con->useEncryption(false);
$con->connect();
$con->processUntil('session_start');
$con->presence();
$roster=$con->roster->getRoster();
var_dump($roster);
//$con->processUntil('roster_received');
if (strpos($_POST['msg'],'CamMode')!==false)
{
$con->message("user#host" ,$_POST['msg']);
}
else
{
$con->message("user#host",$_POST['msg']);
}
$con->disconnect();
}
catch(XMPPHP_Exception $e)
{
die($e->getMessage());
}
messages are successfully sent but dump of $roster is empty. What's wrong?

I add this : $con->processUntil(array('session_start', 'roster_received')); and $con->processTime(5); It worked for me.
...
$con->connect();
$payloads = $con->processUntil(array('session_start', 'roster_received'));
$con->presence();
$con->processTime(5);
$roster = $con->roster->getRoster();
...

Related

Create contact or update existing one depending if they already exist

I'm using Send In Blue to keep track of my contacts. Throughout my site I'm either updating the contact or creating a new one but I've never had to combine the two. I have my pieces of code separately below.
How can I combine the two so that if there's a user there already then it updates them and if not then it creates them?
Is it possible to update first and if that fails create them?
try {
$updateContact = new \SendinBlue\Client\Model\UpdateContact($data);
$result = $SendInBlue->updateContact($email, $updateContact);
return true;
} catch (Exception $e) {
}
And this is the code to create a contact:
try {
$createContact = new \SendinBlue\Client\Model\CreateContact($data);
$result = $SendInBlue->createContact($email, $createContact);
return true;
} catch (Exception $e) {
}
You can remove the updateContact() and just use createContact() because the is an updateEnabled option that can be set during the creation attempt. The purpose of this option is to "Facilitate to update the existing contact in the same request (updateEnabled = true)"
You can either set this while creating the CreateContact object ...
$data["updateEnabled"] = true;
$createContact = new \SendinBlue\Client\Model\CreateContact($data);
Or by using the setUpdateEnabled() method ...
try {
$createContact = new \SendinBlue\Client\Model\CreateContact($data);
$createContact->setUpdateEnabled(true);
$result = $SendInBlue->createContact($email, $createContact);
return true;
} catch (Exception $e) {
}
For reference, here is the code for the CreateContact class that you're using, and here is the documentation for the raw SendInBlue API.

Soundcloud API Check if a user is following another user

I'm trying to figure out if a user is following another user on Soundcloud using the Soundcloud API and php.
So far I came across a solution which would either return an object (user) or a 404 error:
$test = json_decode($client->get('/users/{id1}/followers/{id2}'));
I've tried it multiple times with different user IDs but I always receive a the following error message:
'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 404.'
I know that this is supposed to be the error message which informs me that user2 is not following user1. However I've tried this snippet with ids where I know a reciprocal following exists for sure.
Any suggestions on how this can be solved?
Update (21.05.15):
I've read through some of the Soundcloud documentation and cam across a code snippet:
<?php
require_once 'Services/Soundcloud.php';
// create a client object with access token
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setAccessToken('YOUR_ACCESS_TOKEN');
// Follow user with ID 3207
$client->put('/me/followings/3207');
// Unfollow the same user
$client->delete('/me/followings/3207');
// check the status of the relationship
try {
$client->get('/me/followings/3207');
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
if ($e->getHttpCode() == '404')
print "You are not following user 3207\n";
}
?>
This is pretty much what I was referring to. However if I open a php page with this script the result is always one of three cases:
You are not following user 3207 (expected output)
No output (I'm following the user)
Uncaught exception 'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 404.'
The third option is either referring to $client->put or $client->delete
Here is how i would do this:
<?php
require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud(
'xxxxxxxxxxxxxxxxxxx160', 'xxxxxxxxxxxxxxxxxx34dd1 ');
$userid = 1672444;
$followerid = 383228;
$yesno = '';
try {
$response = json_decode($client->get('users/'.$userid.'/followers'), true);
$yesno = IdInArray($response, $followerid);
echo $yesno;
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
function IdInArray($response, $followerid){
echo $followerid.'<br/>';
for($i = 0; $i < count($response); ++$i) {
if($response[$i]['id'] == $followerid){
return 'yolo';
}
else{
return 'nolo';
}
}
}
?>

Facebook Graph API Insights Requests Return Zero

I am attempting to retrieve the number of fans for my Page via the PHP SDK. Here is all of my code so far.
$fbSession = \Facebook\FacebookSession::newAppSession(self::APP_ID, self::APP_SECRET);
$fbLikesResponse = null;
try {
$fbLikesRequest = new \Facebook\FacebookRequest($fbSession, 'GET', '/{myRealPageIDGoesHere}/insights/page_fans');
$fbLikesResponse = $fbLikesRequest->execute()->getGraphObject()->asArray();
} catch (\Facebook\FacebookRequestException $ex) {
$resp = new \stdClass();
$resp->error = $ex;
return $resp;
} catch (\Exception $ex) {
echo $ex->getMessage();
}
//Add items to response and to store
var_dump($fbLikesResponse);
I never get authentication/authorizaiton errors, but the data response value is always just 0. I can see on the Facebook page itself that there are more than 0 likes.
I really don't want to have to resort to screen-scraping to obtain how many likes my Facebook page has. Any idea why I only get 0s?
Clarification- this is for a server-side call for an analytics engine. No user authentication is involved, so I'm trying to figure out how to get hit the {node}/insights/page_fans endpoint without faking some sort of user login.

Bitcoin sendfrom not returning any errors

I am trying to validate bitcoin engine respond once executing with correct amount within the account balance and correct wallet I am getting transaction ID, but if I enter amount too much and fake wallet I am not receiving any error in return just a blank page with html, head and body elements. Is there is any debug mode or what I can do to receive any response?
$message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
I am using jsonRPCClient to connect with bitcoin engine.
however when I do that in console using RPC commands
I am getting this : Account has insufficient funds (code -6)
code for redirection
if ($message !== '') {
ob_start();
header("Location: mywallet.php?error=done");
die();
} else {
ob_start();
header("Location: mywallet.php?error=true");
die();
}
Update Yes correct I will more ob_start(); above, thing is that once trying (try,catch) event I am getting blank page upon SUCCESS (so not transaction ID like with normal way I am doing I am getting back transaction ID) upon FAIL I am getting Unable to connect to Bitcoin Server. What I just need sounds very simple, how do I verified that transaction is SUCCESSFUL or FAIL, SUCCESSFUL -> I got ID in return, FAIL -> I have got Error in return. SO I can divert users to right places on the page after form is submitted. Actualy I am doing is withdraw funds form, where user inserts amount and his wallet to get funds back from bitcoin account to his private account. Hope this will helps to understand.
UPDATE 2 I have changed construction for that and seems to is working very well, basically is looking for "Unable" word as transaction ID doesn't have that word and other exception I am getting is "Unable to connect to server..." Thank you for guiding me. Any feedback ?
try {
$message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
}
catch (Exception $e) {
$e->getMessage();
}
// exit;
if (!strpos($e,'Unable') !== false){
header("Location: mywallet.php?error=done");
die();
} else {
header("Location: mywallet.php?error=true");
die();
}
What bitcoin php library are you using? Looks like maybe this one?
If that's the case, it doesn't return an error message, it throws BitCoinClientException
So you need something like
try {
$message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
}
catch (Exception $e) {
echo $e->getMessage();
}
Updating
The ob_start seems superfluous because you don't output anything before the header location. Unless you have output something before you've reached this point in which case you can't send a header. So you'd need to move the ob_start up towards the top of the script before any output.
Also you aren't sending message to the wallet.php script. Or are you done with it at that point?
RE: update 2
One thing I might add is the possibility of some other exception message occuring we haven't thought of yet that doesn't contain the "Unable." I'd do something more like:
$errorOccured = false;
try {
$message = ($bitcoin->sendfrom($mywallet,$to_wallet,$wammount));
}
catch (Exception $e) {
$errrorMessage = $e->getMessage();
$errorOccured = true;
}
if (!$errorOccured) {
...
}
else {
header("Location: mywallet.php?error=true&errormsg=" . $errorMessage);
...
}
This would require modifying mywallet.php to accept $errorMessage as an additional GET parameter so you can send it back to the user. Might be nice to additionally use another parameter for sending $message on success which would contain the transaction ID.

graph api is not returning users details suddenly for me. (might be from today)

It is web base application.
The first page is asking for login. Once they click the login button, everything is going on with ajax.
In the ajax request,
$user = $facebook->getUser();
This is giving proper user id.
if ($user) {
try {
$fbme = $facebook->api('/me');
$user_id = $fbme['id'];
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
The below line was working before.
Now it is end in the catch block. So I am not able the user details.
What could be the problem?
Since I didn't make changes in the script so far, It is suddenly stopped working.
You access token may be out of date, I suggest something like this:
try {
$data['user_profile'] = $profile = $this->facebook->api('/me');
}catch (Exception $e){
$this->facebook->setAccessToken($this->facebook->getAccessToken());
try {
if($this->facebook->getUser()) {
$this->facebook->api(array('method' =>'auth.revokeAuthorization' ));
}
}catch(Exception $e) {
redirect(...);
}
redirect(...);
}
It was a facebook bug. They fixed it last night. If you update your facebook php SDK it will work.

Categories