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.

62 comments:

  1. Thanxx A lottttttttttttttttttttttttt ...............

    ReplyDelete
  2. : CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in how solve this

    ReplyDelete
    Replies
    1. http://stackoverflow.com/questions/3349280/php-replacement-for-safe-mode

      This should helps you.

      Delete
  3. Message Sucessfully send to : 9714158984.
    but i will not send message on mobile what is problem of this code plz say me quickly

    ReplyDelete
  4. Hey i get this error

    Notice: Undefined offset: 0 in wamp\www\way2smsapimaster\way2sms.php on line 72

    ReplyDelete
    Replies
    1. Hi Dhaval Desai, it should be because of empty $pharr online 72.
      Make sure that you are passing a valid mobile number.

      Delete
    2. Hey Thanks for your reply. The phone number is valid. I used to use alfreds library uptill now and it worked well. But now it has stopped working. I get a 404 not found error in quicksms.action. When i printed the error this is what i get :

      Array ( [url] => http://site8.way2sms.com/quicksms.action?custid="+custid+"&sponserid="+sponserid+" [content_type] => text/html [http_code] => 404 [header_size] => 232 [request_size] => 515 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.125 [namelookup_time] => 0 [connect_time] => 0.078 [pretransfer_time] => 0.078 [size_upload] => 73 [size_download] => 3581 [speed_download] => 28648 [speed_upload] => 584 [download_content_length] => 3581 [upload_content_length] => 0 [starttransfer_time] => 0.125 [redirect_time] => 0 [certinfo] => Array ( ) )

      Appreciate your help.

      Regards,
      Dhaval

      Delete
    3. API updated please try updated API.

      Delete
  5. message sent successfully. But No message sent to receiver mobile

    ReplyDelete
    Replies
    1. Thanks for your notification!
      I guess , there should some in way2sms side!
      Let me update the correct code soon!

      Delete
    2. API updated please try updated API.

      Delete
  6. It shows message that Message Sucessfully send to : 9595461232.
    But i didn't get any message.

    Plz help

    ReplyDelete
    Replies
    1. Again way2sms team made some change in there code.
      Sure soon let me update the correct one !
      Now I'm on another work!

      Delete
  7. Hi, your demo is working....but when i uploaded into my site and tried i get "Login Faild"

    ReplyDelete
    Replies
    1. Hi , It should work!! make sure that you have entered a correct password? and you server cURL enabled one ?

      Delete
  8. Hi JJ,

    i created way2sms.php file (copied the content you provided) and index.php file (will call the way2sms class, used ur example syntax)

    index.html will have form with names(which will be passed to index.php)

    but its not working, can you help me

    ReplyDelete
  9. how to run the above code....
    when i m running the code in php it displays nothing on the screen......

    ReplyDelete
  10. please tell me how to run the above code in step by step procedure........

    ReplyDelete
    Replies
    1. Hi,
      Create index.php file,
      Copy the above code to it,

      At the end add this code ,

      $obj=new way2sms('Your_way2sms_username','your_way2sms_password');
      $result=$obj->sendSMS('mobile_number','message');
      echo implode('
      ',$result);

      Delete
  11. thanks for you way2sms script its very useful to me

    ReplyDelete
  12. thanks for giving script but,it doesn't work send multiple contacts way2sms
    it's given error can u help me?

    ReplyDelete
  13. error in continue; line in if statement.
    if you remove upper foreach loop then you must remove that continue statement too..

    ReplyDelete
  14. Hi JJ,

    I used ur code to test the functionality with html page, its working.
    i want to use the same code in Flash with PHP, but its not working. can you help me in achieving this?

    FYI, i created way2sms.php in library folder and created way2smsmain.php in services folder ( where i created new function to call the way2sms class and will pass 4 variables to this function)

    Thanks , Srikanth

    ReplyDelete
    Replies
    1. Hi Sri,

      Did you got any error message?
      I'm not familiar with flash sri :) Is that PHP code returns any error message??

      Thanks.

      Delete
    2. PHP code donesnt return any error message, but am not able to make it work through flash. Am using Flash Builder with PHP 4.5 for this.

      Delete
  15. in ur demo, code saying message sent successfully, but we didnt get message. then I tried sending through way2sms.com, i got the message.

    Can you verify the script, may be its not working now.

    ReplyDelete
    Replies
    1. Ok I will.
      Thanks for your notification!

      Delete
    2. Please let me know, once you change any code , i want to know what we need to look for, when we face this type of issues.

      Delete
  16. Hey how to send the sms..
    Please show step by step procedure..

    ReplyDelete
    Replies
    1. See this comment

      http://justtrythis.blogspot.com/2012/06/way2sms-php-api.html?showComment=1361362784463#c8591363861174309267

      Delete
  17. Hi,

    i am getting these if i run the code. But i am not receiving any message.

    Notice: Undefined offset: 0 in C:\xampp\htdocs\waytosms.Php on line 81

    Notice: Undefined offset: 0 in C:\xampp\htdocs\waytosms.Php on line 83

    Notice: Undefined offset: 0 in C:\xampp\htdocs\waytosms.Php on line 105
    Message Sucessfully send to : 9750072295.



    Could u plz help me?

    ReplyDelete
    Replies
    1. Sure it's because some change on way2sms side, Need to update the script.

      Give me some time, Let me update it soon! I'm on another work now!

      Thanks.

      Delete
  18. No Warning But Login Failed

    ReplyDelete
  19. Notice: Undefined offset: 0 in /opt/lampp/htdocs/w2s.php on line 55
    Login Faild

    Please Help Me....I'm in urgent need....

    ReplyDelete
  20. i am unable to login with ur code ..it says login failed..i tried it on my behalf and ur demo site too
    .plz help me

    ReplyDelete
  21. It shows "Login Faild". Username, Password are ok with direct site login. plz help me.

    ReplyDelete
  22. Hi to all,
    Again there is some change in server(way2sms) side. So we have to trace it and update the code.
    Now I'm busy with some other work, so let me update the code soon. Thanks.

    ReplyDelete
  23. Hi, using this code I am unable to login. I passed valid username and password. And curl fun is enabled in server. please suggest me.

    Message - Login Faild

    ReplyDelete
  24. Please help me out..................
    Notice: Undefined offset: 0 in C:\wamp\www\smsf.php on line 55

    the line is : $token = $match[1][0];

    ReplyDelete
    Replies
    1. Message contentStatus1:
      Notice: Undefined offset: 0 in C:\xampp\htdocs\mmarket\source\way2sms.php on line 55
      Login Faild

      Delete
  25. Notice: Undefined offset: 0 in C:\xampp\htdocs\sms\sendsms.php on line 57
    Login Faild

    ReplyDelete
  26. I have Login failed. my username , password is given correctly and my curl function is enabled. but i can't access. Please update new code

    ReplyDelete
  27. I got the Login failed in the ur demo.

    ReplyDelete
  28. Notice: Undefined offset: 0 in line no 59

    $token = $match[1][0];
    pls help me

    ReplyDelete
  29. Message Sucessfully send to : +919688416447....

    but message cannot be delivered .............

    way2sms username and password are correct but not send for sms ....

    so please help me.....

    how to send sms from way2sms to mobile using php?

    please sir

    ReplyDelete
  30. Hello,

    Your demo link is says Login Failed. But I provide right username and password for testing.

    ReplyDelete
  31. synte error at declaration of class

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete
  33. For the institutions which are driven by applications developed using PHP and will to harness the benefits of this trend in their existing business model, are advisable to explore the avenues of SMS API PHP and PHP SMS gateway integration.

    ReplyDelete
  34. Our Ready php code/ script will help you to save time and efforts at a large level. Your enterprise/organization's php application can be easily integrated with our services. However, we provide ready to use bulk SMS API PHP code that helpfuls to send various information to your user's mobile.

    ReplyDelete