Call to a member function processqueue() on a non-object - php

I'm trying to run this file, this file sends emails and updates tables etc (Various tasks).
But, I'm getting this error
Fatal error: Call to a member function processqueue() on a non-object in /home/novosctn/public_html/user/cron_email_handler.php on line 14
I've gone through various questions here but none of them helped me.
What wrong am I doing ?
Any suggestions are appreciated.
DB Structure
indexid(int unsigned) others
1 xyz
2 asa
The file.
<?php
include 'dbconnector.php';
include 'class/class.user.php';
//this file would loop through the Queued rows in the messagequeue and process them.
//first, get all the unprocessed queue.
try
{
$statement = $conn->query("SELECT * from messagequeue where currentstatus = 'Queued'");
$statement->setFetchMode(PDO::FETCH_OBJ);
while($q = $statement->fetch())
{
//now process
if($user->processqueue($q->indexid))
{
//$user->notifyAdmin();
//$user->notifyUser();
$i=2;
//notify admin of a successful queue complete
//notifyuser that a particular queue was not completed.
echo $q->indexid . "was completed";
}
else
{
$i=1;
//the particular que was not completed.
//notify admin about this
echo $q->indexid . "was not completed";
}
}
}
catch(PODException $e)
{
echo $e->getMessage();
}
?>

Related

How to jump from one Function to another in PHP Class

I have a php script to pipe through a mail,
class mailTest
{
// (some code here)
private function saveToDb()
{
// (some code here)
$select = $this->pdo->query("SELECT * FROM tbl_reques WHERE terminal_id = $term AND request_status ='' ");
$select = $select->fetchAll();
if (count($select) > 0) {
echo "Call already Exist (DISCARD)";
} else {
$select_tech = $this->pdo->query("SELECT * FROM tbl_email WHERE terminal_id = $term");
$select_tech = $select_tech->fetchAll();
// (some code here)
}
}
private function sendEmail()
{
$this->today = time();
$this->maildate = date("Y-m-d H:i:s", strtotime('-5 minutes', $this->today));
$select = $this->pdo->query("Select * from tbl_reques WHERE maildate >= '$this->maildate' ");
// some code here
mail($this->from_email, $this->subject, $newmsg, $headers);
}
}
The problem is any time the condition is False i.e echo "Call already Exist (DISCARD)"; The code will not go to the Next Function. i.e the program get halt.
PLS is there a way that if that condition is not met, the program will JUMP to next function for continuation of execution. Or is it possible to use GOTO statement.
Pls what is the best way to handle this in PHP.
Thanks
You have a couple option for this. You can return at the point of failure. Which would exit the function at this point and then do whatever is next in the script being ran. Be sure to do the clean up before your return.
if(count($select) > 0) {
echo "Call already Exist (DISCARD)";
//Clean up if needed
return; //You could also return the message
//or an error code and have another
//evaluation based on that.
} else {
// Or passes
}
You could call the next function but this would be a very bad flow in my opinion
if(count($select) > 0) {
echo "Call already Exist (DISCARD)";
//Clean up if needed
$this->sendEmail();
} else {
// Or passes
}
The reason this would be bad is if say in the script you have
$mailTest = new mailTest();
$mailTest->saveToDb();
$mailTest->sendEmail(); //When the above fails this is called twice.
You could likewise throw an exception
if(count($select) > 0) {
echo "Call already Exist (DISCARD)";
throw new Exception("Call already Exist (DISCARD)");
} else {
// Or passes
}
Now you need to use try and catch
$mailTest = new mailTest();
try {
$mailTest->saveToDb();
catch (Exception $e){
//Do something with $e
//Clean up the failure if needed
}
$mailTest->sendEmail();
There is a finally block as well which would run in cases where your catch stops the script.
PHP in fact has a GOTO statement, see http://php.net/manual/de/control-structures.goto.php
However, it is considered bad style to use it, or in the words of #Konamiman
Unless you are programming in assembler, GOTO should always be treated the same way as the life vest of the airplanes: it is good to have them available, but if you need to use them it means that you are in big trouble.
You can call a function simply by writing its name followed by brackets. In the case of class functions you apply it to $this:
$this->sendEmail();

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';
}
}
}
?>

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

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();
...

PHP pthreads: the lack of resource error

Just got PHP pthreads error
pthreads has detected that the multihread could not be started, the system lacks the necessary resources or the system-imposed limit would be exceeded
and
Cannot initialize zend_mm storage [win32]
in my script...
The PHP code looks like this:
class multihread extends Worker {
public $result;
function __construct($e) {
$this->e = $e;
}
public function run() {
$this->result=file($this->e);
}
}
$threads = 15;
do {
for($i=1; $i<=$threads; $i++) {
if(empty($thread[$i])){
$e=generate_e($i);
if($e==false){
echo "Warning: no more job for e. exiting A"; exit;
}
echo "Starting new thread $i \n";
$thread[$i] = new multihread($e);
$thread[$i]->start(PTHREADS_INHERIT_NONE);
}
elseif($thread[$i]->isWorking()===false) {
if($thread[$i]->result===false){
echo "ERROR:Something wrong with thread $i, exit.";
exit;
}
$thread[$i]->shutdown();
$eval=generate_e($i);
if($e==false){
echo "Warning: no more job exiting B"; exit;
}
$thread[$i] = new multihread($e);
$thread[$i]->start(PTHREADS_INHERIT_NONE);
}
}
usleep(100);
}
while(1);
So, script open new thread, start this thread, then close thread with shutdown() and do it in the loop. It's worked like charm, but after 16000+ opened\closed threads got this error. Seems some resources stay locked? How to fix this?
One shall either detach() the thread before it ends, or call join() on it to wait until it ended.
Missing the one or the other leads to the thread's resources not getting freed and though the system runs out of resources, as observed.

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.

Categories