Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Sunday, June 3, 2012

combine form validation and way2sms class

        In this post I'm going to combine way2sms API with simple form validation. so it makes easy to send sms with nice look. 

Screenshots
sendSms.php
<?php

    session_start();

    if(isset($_POST['userName']) && isset($_POST['passWord']))

    {

        require_once('classSms.php');

        $_SESSION['userName']=$_POST['userName'];

        $_SESSION['passWord']=$_POST['passWord'];

        $smsObj=new way2sms($_POST['userName'],$_POST['passWord']);

        $result=$smsObj->sendSMSToMany($_POST['mobileNumber'],$_POST['msg']);

    }
?>
<html>

<head>

    <title>Consultation Form</title>

    <link rel="stylesheet" type="text/css" href="style.css"/>

    <link href="images/c12.ico" rel="shortcut icon" type="image/x-icon" />

    <script type="text/javascript" language="javascript">

    var regx_number=/^[0-9]{10}$/;

        window.onload = function()

        {

         //onload delegating event

            var inputTag=document.getElementsByTagName("input"),

                form=document.getElementById("getDetails");

            form.onsubmit=validate;

            document.getElementById("msg").onfocus=clearInfo;

            document.getElementById("msg").onblur=showInfo;

            for(var i=0;i<inputTag.length;i++)

            {

                inputTag[i].onfocus=clearInfo;

                inputTag[i].onblur=showInfo;

            }

        

        }

        function showInfo()

        {

            var info=document.getElementById("info");

            info.innerHTML="";

            switch(this.name)

            {

                case "userName":

                    if(!validNumber(this.value)) this.value="Your Mobile Number";

                    break;

                case "passWord":

                    if((this.value)=="") this.value="your password";

                    break;

                case "mobileNumber":

                    if((this.value)=="") this.value="Mobile Numbers";

                    break;

                case "msg":

                    if(this.value =="" ) this.value="Content Here";

                    break;

                default:

                    break;

            }

        }

        function clearInfo()

        {

            var info=document.getElementById("info");

            switch(this.value)

            {

                case "Your Mobile Number":

                    this.value="";

                    info.innerHTML="Enter your way2sms user name(mobile number).";

                    break;

                case "your password":

                    info.innerHTML="Enter your passWord.";

                    this.value="";

                    break;

                case "Mobile Numbers":

                    info.innerHTML="Enter recipient mobile numbers.<br>It should be seperated by comma(,).";

                    this.value="";

                    break;

                case "Content Here":

                    info.innerHTML="Enter message to send.";

                    this.value="";

                    break;

                default:

                    break;

            }

        }

        function validate()

        { 

            var uName=document.getElementById("userName").value,

                pass=document.getElementById("passWord").value,

                mnumber=document.getElementById("mobileNumber").value,

                msg=document.getElementById("msg").value;

            if(validNumber(uName) && (pass != "") && (mnumber != "") )

                return true;

            return false;

        }

        function validNumber(number)

        {

            return regx_number.test(number);

        }

    </script>

</head>

<body>

    <div id="container">

    <div style="margin-left: 80px;"><img alt="" src="images/fill_your_details.png"></div>

        <div id="info"><?php echo @implode("<br/>",$result); ?></div>

        <form name="getDetails" id="getDetails" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">

            <input type="text" name="userName" id="userName" value="<?php if(isset($_SESSION['userName'])) echo $_SESSION['userName']; else { echo "Your Mobile Number"; } ?>"/>

            <input type="password" name="passWord" id="passWord" value="<?php if(isset($_SESSION['passWord'])) echo $_SESSION['passWord']; else { echo "your password"; } ?>"/>

            <input type="text" name="mobileNumber" id="mobileNumber" value="Mobile Numbers"/>

            <textarea name="msg" id="msg">Content Here</textarea>

            <button>send via way2sms</button>

        </form>

    </div>

</body>

</html>
click here for demo.
click here for classSms.php.
click here for simple effective form validation. 
 

Friday, June 1, 2012

Way2Sms PHP CURL API [OUTDATED]

The following class help you to send sms via way2sms gateway.

Features of this class:
  • Can send sms to mutiple contacts at one shot , each contact should be seprated by comma (,)
  • Can send long sms (it automatically split text by 160 char/sms)
way2sms.php


<?php/*way2sms
 *used to send sms using web service
 *@ $uid       String:  username to authenticate the way2sms website
 *@ $pwd       String:  password for way2sms website
 *@ $phone     String:  phone numbers of recipients
 *@ $msg       String:  the message to be send
 */class way2sms{
    var $uid,$pwd,$phone,$msg;
    /*
    *@ constructor of way2sms class
    *
    *@ $id     String :  username to authenticate the way2sms website
    *@ $pass   String :  password for way2sms website
    *
    */
    function way2sms($id,$pass)
    {
        $this->uid=urlencode($id);
        $this->pwd=urlencode($pass);
    }
    /*
    *sendSMSToMany
    *@description : to send the sms
    *
    *@ $phone    String :  phone numbers of recipients
    *@ $msg      String :  the message to be send
    *
    *@return     array :  error message or success message
    */
    
    function sendSMS($phone$msg)
    {
        $curl curl_init();
        $timeout 30;
        $result = array();
        $autobalancer=rand(1,8);

        curl_setopt ($curlCURLOPT_URL"http://site".$autobalancer.".way2sms.com/Login1.action");
        curl_setopt ($curlCURLOPT_POST1);
        curl_setopt ($curlCURLOPT_POSTFIELDS"username=" $this->uid "&password=" $this->pwd);
        curl_setopt ($curlCURLOPT_COOKIESESSION1);
        curl_setopt ($curlCURLOPT_COOKIEFILE"cookie_way2sms");
        curl_setopt ($curlCURLOPT_FOLLOWLOCATION1);
        curl_setopt ($curlCURLOPT_MAXREDIRS20);
        curl_setopt ($curlCURLOPT_RETURNTRANSFER1);
        curl_setopt ($curlCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
        curl_setopt ($curlCURLOPT_CONNECTTIMEOUT$timeout);
        curl_setopt ($curlCURLOPT_REFERER"http://site".$autobalancer.".way2sms.com/");
        $text curl_exec($curl);
        
        preg_match_all('/<input[\s]*type="hidden"[\s]*name="Token"[\s]*id="Token"[\s]*value="?([^>]*)?"/si'$text$match);
        $token $match[1][0]; 

        // Check for proper login
        $pos stripos(curl_getinfo($curlCURLINFO_EFFECTIVE_URL), "main.action");

        if ($pos === "FALSE" || $pos == || $pos == "") return array("Login Faild");

        if (trim($msg) == "" || strlen($msg) == 0) return array("Invalid message");
        
        $msgs $this->split_for_sms($msg);
        
        //$pharr = explode(",",$phone);
        $p=$phone;
        $refurl curl_getinfo($curlCURLINFO_EFFECTIVE_URL);
        $refurl_instantsms=0;
        // foreach ($pharr as $p)
        // {
            if (strlen($p) != 10 || !is_numeric($p) || strpos($p".") != false)
            {
                array_push($result,"Invalid number : " $p ". ");
                //continue;
            }
            foreach($msgs as $msg)
            {
                curl_setopt ($curlCURLOPT_REFERER$refurl);
                curl_setopt ($curlCURLOPT_URL"http://site".$autobalancer.".way2sms.com/jsp/SingleSMS.jsp?Token=".$token);
                $text curl_exec($curl);
                
                preg_match_all('/rqMob=["\']([\d\w]+)["\'];/si'$text$match);
                $rqMob=$match[1][0];
                preg_match_all('/rqTok=["\']([\d\w]+)["\'];/si'$text$match);
                $rqTok=$match[1][0];
                //preg_match_all('/tkn=["\']?([\d\w\W]+)?["\'];/si', $text, $match);
                $Token=$token;
                /* preg_match_all('/<input[\s]*type="hidden"[\s]*name="cgnr"[\s]*id="cgnr"[\s]*value="?([^>]*)?"/si', $text, $match);
                $cgnr=$match[1][0];
                preg_match_all('/<input[\s]*type="hidden"[\s]*name="wasup"[\s]*id="wasup"[\s]*value="?([^>]*)?"/si', $text, $match); */
                preg_match_all('/[\s]*name=["\']wasup["\'][\s]*id=["\']wasup["\'][\s]*value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/name=["\']wasup["\'][\s]*id=["\']wasup["\'][\s]*><option value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/[\s]*name=["\']wasup["\'][\s]*id=["\']wasup["\'][\s]*>?([^>]*)?</si'$text$match);
                $wasup=$match[1][0];
                
                preg_match_all('/[\s]*name=["\']nrc["\'][\s]*id=["\']nrc["\'][\s]*value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/name=["\']nrc["\'][\s]*id=["\']nrc["\'][\s]*><option value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/[\s]*name=["\']nrc["\'][\s]*id=["\']nrc["\'][\s]*>?([^>]*)?</si'$text$match);
                $nrc=$match[1][0];
                preg_match_all('/[\s]*name=["\']HiddenAction["\'][\s]*value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/name=["\']HiddenAction["\'].*><option value=["\']?([^>]*)?["\']/si'$text$match);
                $HiddenAction=$match[1][0];
                preg_match_all('/[\s]*name=["\']hud_pani["\'][\s]*id=["\']hud_pani["\'][\s]*value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/name=["\']hud_pani["\'][\s]*id=["\']hud_pani["\'][\s]*><option value=["\']?([^>]*)?["\']/si'$text$match);
                if(!isset($match[1][0]))
                    preg_match_all('/[\s]*name=["\']hud_pani["\'].*id=["\']hud_pani["\'][\s]*>?([^>]*)?</si'$text$match);
                $hud_pani=$match[1][0];
                
                preg_match_all('/[\s]*name=["\']catnamedis["\'][\s]*id=["\']catnamedis["\'][\s]*value=["\']?([^>]*)?["\']/si'$text$match);
                $catnamedis $match[1][0];
                preg_match_all('/[\s]*id=["\']diffNo["\'][\s]*name=["\']diffNo["\'][\s]*value=["\']?([^>]*)?["\']/si'$text$match);
                $diffNo $match[1][0];
                
                /* preg_match_all('/<select[\s]*style="display: none;"[\s]*name="action"[\s]*id="action%>"[\s]*><option[\s]*value="?([^>]*)?"/si', $text, $match);
                $action = $match[1][0]; // get custid from the form fro the Action field in the post form                
                preg_match_all('/<textarea[\s]*style="display: none;"[\s]*name="Token"[\s]*id="Token">?([^>]*)?<\/textarea>/si', $text, $match);
                $Token = $match[1][0]; */
                
                $p urlencode($p);
                
                if($refurl_instantsms == 0)
                {
                    $refurl_instantsms=curl_getinfo($curlCURLINFO_EFFECTIVE_URL);
                }
            
            // Send SMS
            
                curl_setopt ($curlCURLOPT_URL"http://site".$autobalancer.".way2sms.com/jsp/stp2p.action");
                curl_setopt ($curlCURLOPT_REFERER$refurl_instantsms);
                curl_setopt ($curlCURLOPT_POST1);
                
                curl_setopt ($curlCURLOPT_POSTFIELDS"nrc=$nrc&wasup=$wasup&HiddenAction=$HiddenAction&hud_pani=$hud_pani&catnamedis=$catnamedis&$rqTok=$Token&$rqMob=$p&textArea=$msg&diffNo=$diffNo");
                curl_exec($curl);
                //var_dump(curl_getinfo($curl));
                @$sendError .= curl_error($curl);
            }
            array_push($result,"Message Sucessfully send to : " $p ". ");
        //}
        
        // Logout
        curl_setopt ($curlCURLOPT_URL"http://site".$autobalancer.".way2sms.com/jsp/logout.jsp");
        curl_setopt ($curlCURLOPT_REFERER$refurl);
        $text curl_exec($curl);    
        
        curl_close($curl);
        if (!empty($sendError)){
            return array($sendError);
        }
        return $result;
    }
    function split_for_sms($msg)
    {
        $length=strlen($msg);
        if($length <= 160)
        {
            return array(urlencode($msg));
        }
        else
        {
            preg_match_all("~.{0,153}(\s|$)~",$msg,$matches);
            $matches=array_filter($matches[0]);
            foreach($matches as $key=>$match)
                $matches[$key]=urlencode("PART-".($key+1).":".$matches[$key]);
            return $matches;
        }
    }
}
?>


Example usage of this class:
$obj=new way2sms('myusername','mypassword');
$result=$obj->sendSMS('mobile_number','message');
echo implode('<br >',$result);
click here for demo.