Jump to content

i want to assign values from database to variables in a class


Recommended Posts

hi

i am creating a sms module. for that i am using clickatell sms class available online.

in the bottom code notice the 6th line

which is var $api_id = "3434343";

here the value is hard coded.

 

i want to assign the values to the class variables from database

such as

var $api_id = $apiid;

 

i have created a sms setting table in mysql, which would contain the username, password, api details

and i want to assign that values from database to these class variables.

which are

    var $api_id = "3434343";

    var $user = "username";

    var $password = "9987987";

 

i want something like this

 

    var $user = $username;

    var $password = $password;

    var $api_id = $apiid;

 

but it is causing problem for me if i assign them like this.

 

please help me how can i assign the database values to these class variables.

i tried everything but i was unable to assign the $variables to class variables.

 

$username, $password, $api are called from a include file called includes/smssetttings.php on the top of the class file which in turns queries the database for the same.

 

Thanks

Amardeep

 

<?php
include "../includes/smssettings.php";

class sms {

    /**
    * Clickatell API-ID
    * @var integer
    */
    var $api_id = "3434343";

    /**
    * Clickatell username
    * @var mixed
    */
    var $user = "username";

    /**
    * Clickatell password
    * @var mixed
    */
    var $password = "9987987";

    /**
    * Use SSL (HTTPS) protocol
    * @var bool
    */
    var $use_ssl = false;

    /**
    * Define SMS balance limit below class will not work
    * @var integer
    */
    var $balace_limit = 0;

    /**
    * Gateway command sending method (curl,fopen)
    * @var mixed
    */
    var $sending_method = "fopen";

    /**
    * Optional CURL Proxy
    * @var bool
    */
    var $curl_use_proxy = false;

    /**
    * Proxy URL and PORT
    * @var mixed
    */
    var $curl_proxy = "http://127.0.0.1:8080";

    /**
    * Proxy username and password
    * @var mixed
    */
    var $curl_proxyuserpwd = "login:secretpass";

    /**
    * Callback
    * 0 - Off
    * 1 - Returns only intermediate statuses
    * 2 - Returns only final statuses
    * 3 - Returns both intermediate and final statuses
    * @var integer
    */
    var $callback = 0;

    /**
    * Session variable
    * @var mixed
    */
    var $session;

    /**
    * Class constructor
    * Create SMS object and authenticate SMS gateway
    * @return object New SMS object.
    * @access public
    */
    function sms () {
        if ($this->use_ssl) {
            $this->base   = "http://api.clickatell.com/http";
            $this->base_s = "https://api.clickatell.com/http";
        } else {
            $this->base   = "http://api.clickatell.com/http";
            $this->base_s = $this->base;
        }

        $this->_auth();
    }

    /**
    * Authenticate SMS gateway
    * @return mixed  "OK" or script die
    * @access private
    */
    function _auth() {
    	$comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
        $this->session = $this->_parse_auth ($this->_execgw($comm));
    }

    /**
    * Query SMS credis balance
    * @return integer  number of SMS credits
    * @access public
    */
    function getbalance() {
    	$comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
        return $this->_parse_getbalance ($this->_execgw($comm));
    }

    /**
    * Send SMS message
    * @param to mixed  The destination address.
    * @param from mixed  The source/sender address
    * @param text mixed  The text content of the message
    * @return mixed  "OK" or script die
    * @access public
    */
    function send($to=null, $from=null, $text=null) {

    	/* Check SMS credits balance */
    	if ($this->getbalance() < $this->balace_limit) {
    	header("Location: smsflags.php?msg=".urlencode("Your SMS Credit Balance Is Low"));
	exit();
    	    //die ("You have reach the SMS credit limit!");
    	};

    	/* Check SMS $text length */
        if (strlen ($text) > 465) {
    	header("Location: smsflags.php?msg=".urlencode("Your Message Is Too Long"));
	exit();
    	//die ("Your message is to long! (Current lenght=".strlen ($text).")");
    	}

    	/* Does message need to be concatenate */
        if (strlen ($text) > 160) {
            $concat = "&concat=3";
    	} else {
            $concat = "";
        }

    	/* Check $to and $from is not empty */
        if (empty ($to)) {
       	header("Location: smsflags.php?msg=".urlencode("Please Specify Destination Mobile Number"));
	exit();
    	//die ("You not specify destination address (TO)!");
    	}
        if (empty ($from)) {
        header("Location: smsflags.php?msg=".urlencode("Please Specify Source Address"));
	exit();
    	//die ("You not specify source address (FROM)!");
    	}

    	/* Reformat $to number */
        $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
        $to = str_replace($cleanup_chr, "", $to);

    	/* Send SMS now */
    	$comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s%s",
            $this->base,
            $this->session,
            rawurlencode($to),
            rawurlencode($from),
            rawurlencode($text),
            $this->callback,
            $concat
        );
        return $this->_parse_send ($this->_execgw($comm));
    }

    /**
    * Execute gateway commands
    * @access private
    */
    function _execgw($command) {
        if ($this->sending_method == "curl")
            return $this->_curl($command);
        if ($this->sending_method == "fopen")
            return $this->_fopen($command);
        header("Location: smsflags.php?msg=".urlencode("Unsupported Sending Method"));
	exit();
        //die ("Unsupported sending method!");
    }

    /**
    * CURL sending method
    * @access private
    */
    function _curl($command) {
        $this->_chk_curl();
        $ch = curl_init ($command);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
        if ($this->curl_use_proxy) {
            curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy);
            curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
        }
        $result=curl_exec ($ch);
        curl_close ($ch);
        return $result;
    }

    /**
    * fopen sending method
    * @access private
    */
    function _fopen($command) {
        $result = '';
        $handler = @fopen ($command, 'r');
        if ($handler) {
            while ($line = @fgets($handler,1024)) {
                $result .= $line;
            }
            fclose ($handler);
            return $result;
        } else {
        header("Location: smsflags.php?msg=".urlencode("Error while executing fopen sending method !<br />Please check that does PHP have OpenSSL support and the PHP version is greater than 4.3.0."));
	exit();
        //die ("Error while executing fopen sending method!<br>Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0.");
        }
    }

    /**
    * Parse authentication command response text
    * @access private
    */
    function _parse_auth ($result) {
    	$session = substr($result, 4);
        $code = substr($result, 0, 2);
        if ($code!="OK") {
        header("Location: smsflags.php?msg=".urlencode("Error In SMS Authorization ($result)"));
	exit();
        //die ("Error in SMS authorization! ($result)");
        }
        return $session;
    }

    /**
    * Parse send command response text
    * @access private
    */
    function _parse_send ($result) {
    	$code = substr($result, 0, 2);
    	if ($code!="ID") {
    	header("Location: smsflags.php?msg=".urlencode("Error Sending SMS ! ($result)"));
	exit();
    	//die ("Error sending SMS! ($result)");
    	} else {
    	$code = "OK";
    	}
        return $code;
    }

    /**
    * Parse getbalance command response text
    * @access private
    */
    function _parse_getbalance ($result) {
    	$result = substr($result, ;
        return (float)$result;
    }

    /**
    * Check for CURL PHP module
    * @access private
    */
    function _chk_curl() {
        if (!extension_loaded('curl')) {
        header("Location: smsflags.php?msg=".urlencode("This SMS API class can not work without CURL PHP module !. Try using fopen sending method."));
	exit();
        //die ("This SMS API class can not work without CURL PHP module! Try using fopen sending method.");
        }
    }
}

?>

Link to comment
Share on other sites

You could add the assigning of the user, pass, etc to the construct of the class (in this case, php4, the sms method) to be set when the class is instantiated...

 

such as

<?php
$sms = new sms($username, $password, $apiid);
?>

 

and in the class add the assignment to your construct

 

<?php
function sms($username, $password, $api_id)
{
   $this->username = $username;
   $this->password = $password;
   $this->api_id = $api_id;

   // The other stuff that's in your function already
}
?>

 

This is the best way of doing things since I figured you never want an instance of sms to be without a username, password, and api_id.

Link to comment
Share on other sites

i tried but it is not working

 

below is the code of class file api.php

i have added the function sms and commented the var $user, var $password etc.

 

also i have added

 

$sms = new sms($username, $password, $apiid); in procsendmsg.php

as

$mysms = new sms($username, $password, $apiid); in procsendmsg.php

 

code of which is at the end

 

please guide me how can i assign the database variables to class variables.

 

thanks for helping me buddy, thanks a lot.

plz reply.

 

<?php

class sms {
function sms($ausername, $apassword, $apiid, $minbalance)
{
   $this->user = $ausername;
   $this->password = $apassword;
   $this->api_id = $apiid;
   $this->$balace_limit = $minbalance;
   // The other stuff that's in your function already
}

    /**
    * Clickatell API-ID
    * @var integer
    */
    //var $api_id = "45654645";

    /**
    * Clickatell username
    * @var mixed
    */
    //var $user = "54634545";

    /**
    * Clickatell password
    * @var mixed
    */
    //var $password = "456546546";

    /**
    * Use SSL (HTTPS) protocol
    * @var bool
    */
    var $use_ssl = false;

    /**
    * Define SMS balance limit below class will not work
    * @var integer
    */
    //var $balace_limit = 0;

    /**
    * Gateway command sending method (curl,fopen)
    * @var mixed
    */
    var $sending_method = "fopen";

    /**
    * Optional CURL Proxy
    * @var bool
    */
    var $curl_use_proxy = false;

    /**
    * Proxy URL and PORT
    * @var mixed
    */
    var $curl_proxy = "http://127.0.0.1:8080";

    /**
    * Proxy username and password
    * @var mixed
    */
    var $curl_proxyuserpwd = "login:secretpass";

    /**
    * Callback
    * 0 - Off
    * 1 - Returns only intermediate statuses
    * 2 - Returns only final statuses
    * 3 - Returns both intermediate and final statuses
    * @var integer
    */
    var $callback = 0;

    /**
    * Session variable
    * @var mixed
    */
    var $session;

    /**
    * Class constructor
    * Create SMS object and authenticate SMS gateway
    * @return object New SMS object.
    * @access public
    */
    function sms () {
        if ($this->use_ssl) {
            $this->base   = "http://api.clickatell.com/http";
            $this->base_s = "https://api.clickatell.com/http";
        } else {
            $this->base   = "http://api.clickatell.com/http";
            $this->base_s = $this->base;
        }

        $this->_auth();
    }

    /**
    * Authenticate SMS gateway
    * @return mixed  "OK" or script die
    * @access private
    */
    function _auth() {
    	$comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
        $this->session = $this->_parse_auth ($this->_execgw($comm));
    }

    /**
    * Query SMS credis balance
    * @return integer  number of SMS credits
    * @access public
    */
    function getbalance() {
    	$comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
        return $this->_parse_getbalance ($this->_execgw($comm));
    }

    /**
    * Send SMS message
    * @param to mixed  The destination address.
    * @param from mixed  The source/sender address
    * @param text mixed  The text content of the message
    * @return mixed  "OK" or script die
    * @access public
    */
    function send($to=null, $from=null, $text=null) {

    	/* Check SMS credits balance */
    	if ($this->getbalance() < $this->balace_limit) {
    	header("Location: smsflags.php?msg=".urlencode("Your SMS Credit Balance Is Low"));
	exit();
    	    //die ("You have reach the SMS credit limit!");
    	};

    	/* Check SMS $text length */
        if (strlen ($text) > 465) {
    	header("Location: smsflags.php?msg=".urlencode("Your Message Is Too Long"));
	exit();
    	//die ("Your message is to long! (Current lenght=".strlen ($text).")");
    	}

    	/* Does message need to be concatenate */
        if (strlen ($text) > 160) {
            $concat = "&concat=3";
    	} else {
            $concat = "";
        }

    	/* Check $to and $from is not empty */
        if (empty ($to)) {
       	header("Location: smsflags.php?msg=".urlencode("Please Specify Destination Mobile Number"));
	exit();
    	//die ("You not specify destination address (TO)!");
    	}
        if (empty ($from)) {
        header("Location: smsflags.php?msg=".urlencode("Please Specify Source Address"));
	exit();
    	//die ("You not specify source address (FROM)!");
    	}

    	/* Reformat $to number */
        $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
        $to = str_replace($cleanup_chr, "", $to);

    	/* Send SMS now */
    	$comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s%s",
            $this->base,
            $this->session,
            rawurlencode($to),
            rawurlencode($from),
            rawurlencode($text),
            $this->callback,
            $concat
        );
        return $this->_parse_send ($this->_execgw($comm));
    }

    /**
    * Execute gateway commands
    * @access private
    */
    function _execgw($command) {
        if ($this->sending_method == "curl")
            return $this->_curl($command);
        if ($this->sending_method == "fopen")
            return $this->_fopen($command);
        header("Location: smsflags.php?msg=".urlencode("Unsupported Sending Method"));
	exit();
        //die ("Unsupported sending method!");
    }

    /**
    * CURL sending method
    * @access private
    */
    function _curl($command) {
        $this->_chk_curl();
        $ch = curl_init ($command);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
        if ($this->curl_use_proxy) {
            curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy);
            curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
        }
        $result=curl_exec ($ch);
        curl_close ($ch);
        return $result;
    }

    /**
    * fopen sending method
    * @access private
    */
    function _fopen($command) {
        $result = '';
        $handler = @fopen ($command, 'r');
        if ($handler) {
            while ($line = @fgets($handler,1024)) {
                $result .= $line;
            }
            fclose ($handler);
            return $result;
        } else {
        header("Location: smsflags.php?msg=".urlencode("Error while executing fopen sending method !<br />Please check that does PHP have OpenSSL support and the PHP version is greater than 4.3.0."));
	exit();
        //die ("Error while executing fopen sending method!<br>Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0.");
        }
    }

    /**
    * Parse authentication command response text
    * @access private
    */
    function _parse_auth ($result) {
    	$session = substr($result, 4);
        $code = substr($result, 0, 2);
        if ($code!="OK") {
        header("Location: smsflags.php?msg=".urlencode("Error In SMS Authorization ($result)"));
	exit();
        //die ("Error in SMS authorization! ($result)");
        }
        return $session;
    }

    /**
    * Parse send command response text
    * @access private
    */
    function _parse_send ($result) {
    	$code = substr($result, 0, 2);
    	if ($code!="ID") {
    	header("Location: smsflags.php?msg=".urlencode("Error Sending SMS ! ($result)"));
	exit();
    	//die ("Error sending SMS! ($result)");
    	} else {
    	$code = "OK";
    	}
        return $code;
    }

    /**
    * Parse getbalance command response text
    * @access private
    */
    function _parse_getbalance ($result) {
    	$result = substr($result, ;
        return (float)$result;
    }

    /**
    * Check for CURL PHP module
    * @access private
    */
    function _chk_curl() {
        if (!extension_loaded('curl')) {
        header("Location: smsflags.php?msg=".urlencode("This SMS API class can not work without CURL PHP module !. Try using fopen sending method."));
	exit();
        //die ("This SMS API class can not work without CURL PHP module! Try using fopen sending method.");
        }
    }
}

?>

 

code for calling

 

<?
session_start();
require_once ("sms/api.php");
include_once "includes/smssettings.php";
include_once ("admincheck.php");
include_once "includes/panelconfig.php";
include_once "includes/settings.php";
include_once "includes/myfunctions.php";
include "db/dbconnect.php";

$db = mysql_connect($MySQL_Host, $MySQL_User, $MySQL_Pass) or die("could not connect");
mysql_select_db($MySQL_DB,$db);

$mysms = new sms();

$to=trim($_POST['to']);
$from=$afrom;
$message=dwords($_POST['message']);

if ($to=='' || $message=='')
{
header("Location: sendsms.php?msg=".urlencode("Please Enter The Complete Details"));
exit();
}

$_SESSION['to']=$to;
$_SESSION['message']=$message;

$mysms->send($to,$from,$message);

$_SESSION['to']="";
$_SESSION['message']="";

$sql = "insert into ".$tableprefix."sentinstantsmss(ato,amessage,datecreated) values('$to','$message',now())";
if (mysql_query($sql))
{
mysql_close();
header("Location: sendsms.php?msg=".urlencode("SMS Sent Successfully To $to & Saved"));
exit();
}
else
{
echo mysql_error();
mysql_close();
//header("Location: sendsms.php?msg=".urlencode("SMS Sent Successfully To $to"));
//exit();
}
?>

Link to comment
Share on other sites

i am unable to get it.

 

plz help me.

 

i have attached the files.

 

procsendmsg.php is file which sends the sms by calling the class file sms/api.php

 

$mysms = new sms();

$mysms->send($to,$from,$message);

 

this is what i use to send the message.

 

plz tell me what to replace and how so that i can asssign the username, password and api id from database.

 

plz.

you have helped a lot but plz make it happen

i will be thankful to you.

 

i have attached the files for your reference.

 

Amardeep Singh

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.