Tuesday, 26 August 2014

PHP Code for Sending free SMS through your way2sms account executed and working codeUPDATED(5-5-2015)

PHP Code for Sending free SMS through your way2sms account executed and working code

UPDATED(5-5-2015)

Hello friends i am Ravi kumar.J a freelancer web programmer i wrote an application in PHP with requirements  to send a SMS to mobiles without logging in  way2sms service.

when i googled i got many codes related to way2sms which sends free sms through our own application page which properly working with the old version of way2sms.Most of them are not working to the new revised way2sms services. Based on the current requirements i wrote a new PHP application.

  

This is code is executed in my application,by entering  the values like way2sms username, password,receiver mobile number and message(up to 140 characters) in index.html page automatically  sends the messages from your way2sms account to mobiles.

Some Points to Remember...

1.Their should be a registered way2sms account for sender.

2.Cookie file must have full READ and WRITE permission. Give 777 permission in Linux and full read write permission in windows. 

3.Do not include '0' or '+91' before mobile number.

4.maximum message limit is upto 140 characters.

Testing the application:

1.Browse the index.html the following window will appear just enter the desired values.

The resultant output would be like this: 

 

 Program code:

1.example.php

2.index.html

3.class.sms.php

4.class.curl.php

5.cprint.php(it is optional meant for console printing)

6.cookies.txt

  Code follows:

1.example.php

<?php
error_reporting(E_ALL);
ob_implicit_flush(true);

include_once "class.curl.php";
include_once "class.sms.php";
include_once "cprint.php";

$smsapp=new sms();
$smsapp->setGateway('way2sms');
$myno=$_POST['fromno'];
$p=$_POST['pass'];
$tonum=$_POST['tono'];
$mess=$_POST['message'];

cprint("Logging in ..\n");
$ret=$smsapp->login($myno,$p);

if (!$ret) {
   cprint("Error Logging In");
   exit(1);
}

print("Logged in Successfully\n");

print("Sending SMS ..\n");
$ret=$smsapp->send($tonum,$mess);

if (!$ret) {
   print("Error in sending message");
   exit(1);
}

print("Message sent");

?>

2.index.html

<html>
<head>
Sending sms from your webpage</head>
<body>
<h1>SEND SMS</h1>
<h1>LOGIN WITH WAY2SMS</h1>
<form action="example.php" method="post">
<table border="1">
<tr><td>
YOUR MOBILE NO:<input type="text" name="fromno" size="10"></td></tr>
<tr><td>PASSWORD:<input type="password" name="pass"></td></tr>
<tr><td>RECEIVER MOBILE NUMBER:<input type="text" name="tono" size="10"></td></tr>
<tr><td>TYPE MESSAGE:<textarea name="message" rows="10" cols="40">TYPE UPTO 140 CHARACTERS ONLY
</textarea></td></tr>
<tr><td><input type="submit" value="sendSMS"></td></tr>
</table>
</form>
</body>
</html>

3.class.sms.php

<?php

include_once "cprint.php";

class sms
{
    var $username,$password;
    var $curl,$server,$data;

    public function __construct()
    {
        $this->curl=new cURL();
        //$this->curl->setProxy("");
        $this->data=array();
    }

    public function setGateway($serverName)
    {
        switch($serverName)
        {
            case 'way2sms':
            $this->server='way2sms';
            break;
           
            default :
            print "Currently only Way2sms is supported";
            break;
        }
    }
    public function login($username,$password)
    {
        $server=$this->server;
        return(call_user_func(array($this,"login_$server"),$username,$password));
    }

    public function send($number,$msg)
    {
        $server=$this->server;
        return(call_user_func(array($this,"send_$server"),$number,$msg));
    }

    private function login_way2sms($username,$password)
    {
        $html=($this->curl->post("http://www.way2sms.com","1=1"));

        if (!preg_match("/Location:(.*)\n/",$html,$matches)) {
            print("Error getting domain");
            cprint($html);
            return(0);
        }

        $domain=trim($matches[1]);
        $this->data['domain']=$domain;
        cprint("Domain:$domain");

        $html= $this->curl->post(
            "${domain}Login1.action",
            "username=$username&password=$password&Submit=Sign+in"
        );


        if (!preg_match('/<h3>Welcome to Way2SMS<.h3>/',$html)) {
            print("Error Logging In");
            print($html);
            return(0);
        }

        print("Logged In Successfully");

        if (!preg_match("/Location:(.*)[?]id=(.*)\n/",$html,$matches)) {
            print("Error getting location & token");
            cprint($html);
            return(0);
        }

        $referer=trim($matches[1]);
        $token=trim($matches[2]);
        $this->data['referer']=$referer;
        $this->data['token']=$token;
        cprint("Referer:$referer");
        cprint("Token:$token");
        return(1);
    }
   
    
    private function send_way2sms($number,$msg)
    {
        $domain=$this->data['domain'];
        print("Msg:$msg");
        $token=$this->data['token'];

        $html=$this->curl->post(
            "{$domain}main.action?section=s",
            "vfType=register_verify&Token=${token}",
            $this->data['referer']
        );

        $msg=urlencode($msg);
        $html=$this->curl->post(
            "{$domain}smstoss.action",
            "ssaction=ss&Token=${token}&mobile=$number&message=$msg"
        );

        if (!preg_match('/Message has been submitted successfully/',$html)) {
            print("Error in sending sms");
            print($html);
            return(0);
        }
        else {
            echo "<script type=\"text/javascript\">alert('SMS Successfully sended');</script>";
           
            print("sms sended sucessfully");
            print("Logged In Successfully");
            return(1);
        }
    }

}

?>
 

4.class.curl.php

<?php
class cURL {
var $headers;
var $user_agent;
var $compression;
var $cookie_file;
var $proxy;
function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
$this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$this->headers[] = 'Accept-Language: en-us,en;q=0.5';
$this->headers[] = 'Accept-Encoding    gzip,deflate';
$this->headers[] = 'Keep-Alive: 300';
$this->headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$this->user_agent = 'iPhone 4.0';
$this->compression=$compression;
$this->proxy=$proxy;
$this->cookies=$cookies;
if ($this->cookies == TRUE) $this->cookie($cookie);
}

function setUserAgent($ua)
{
   
}
function setProxy($proxy)
{
    $this->proxy=$proxy;
}

function cookie($cookie_file) {
if (file_exists($cookie_file)) {
$this->cookie_file=$cookie_file;
} else {
fopen($cookie_file,'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
$this->cookie_file=$cookie_file;
fclose($this->cookie_file);
}
}

function get($url) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process,CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);   
curl_close($process);
return $return;
}
function post($url,$data,$referer=false) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process, CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
if($referer)
{
curl_setopt($process, CURLOPT_REFERER, $referer);
}
curl_setopt($process, CURLOPT_POST, 1);
  curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
  
    curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 2);
$return = curl_exec($process);
curl_close($process);
return $return;
}
function error($error) {
echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
die;
}
}

?>

5.cprint.php

 <?php

// Print to console

function cprint($str) {
   static $debug_2014=-1;

   if ($debug_2014===-1) {
       if (!empty($GLOBALS["_SERVER"]["DEBUG_2014"])) { $debug_2014=1; }
       else { $debug_2014=0; }
   }
   if ($debug_2014===1) { echo $str,"\n"; }
}
?>

6.cookies.txt

this is empty file


I Sincerely convey my regards to Govind Jujare who encouraged to write this blog and his contribution in successfully executing this code.


 

75 comments:

  1. Its Working... Thanks for your Efforts...

    ReplyDelete
  2. thanks vignesh using my code my doubts contact me for mail id ravikumar509@gmail.com

    ReplyDelete
  3. Thank you so much.. and its working fine..

    ReplyDelete
  4. ok welcome dude,i will send the updated code if necessary

    ReplyDelete
  5. thank you so much ravi..for contact personally

    ReplyDelete
  6. Replies
    1. Bulk sms is not possible,they have many problems,daily we can send 30 sms one by one or at a time we can send 10 sms's only,that is for free.If any one is DoNotDisturb is activated message will be blocked,Solution is that we need to buy sms packages at that time we can send bulk sms.

      Delete
  7. how to send a bulk email...???

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

    ReplyDelete
    Replies
    1. Remove the cookies from browser and than run

      Delete
  9. Warning: include_once(cprint.php) [function.include-once]: failed to open stream: No such file or directory in /home/a2198117/public_html/class.sms.php on line 3

    ReplyDelete
    Replies
    1. Hey mayur put the cprint.php file in the same folder the above error wil goes

      Delete
  10. ok ok but next error is Error getting domain

    ReplyDelete
    Replies
    1. Wt domain error just put that errors,maybe yr server problem

      Delete
  11. hi should i host this on a local server to send sms

    ReplyDelete
  12. Replies
    1. first install wamp,check wheather it is working or not with sample example, then try this example

      Delete
    2. i am also have a same problam i am using online server so it is properly working

      Delete
  13. This comment has been removed by the author.

    ReplyDelete
  14. how to solve of this type problem Error getting domain ?

    ReplyDelete
    Replies
    1. first install wamp,check wheather it is working or not with sample example, then try this example

      Delete
  15. same problem of Error getting domain. I am using xampp. pl guide

    ReplyDelete
  16. Its working proper thanku for this wonderful code.

    ReplyDelete
  17. how can i send unlimited message.

    ReplyDelete
  18. Hello Mr, ravi kumar can you tell me , how can i send unlimited messages.
    kindly reply asp. thanku.

    ReplyDelete
    Replies
    1. We cannot send unlimited SMS, u want to send bulk SMS u have to buy SMS pack, my app is for sending free SMS I.e upto 30/sms per day

      Delete
  19. Can i buy SMS pack from waytosms ?

    ReplyDelete
    Replies
    1. u can buy sms pack again u need to write another php(app) for that application,requirements will changes.

      Delete
  20. Good Morrning Mr, ravi if on place of curl_setopt($process, CURLOPT_TIMEOUT, 30); i will apply curl_setopt($process, CURLOPT_TIMEOUT, 60);
    Is this thing can change sending message quantity, is it right ?

    ReplyDelete
    Replies
    1. i am not checked for 60 seconds,it is for timeout only it will waits for 30,if 30 will finishes it will timed out,check for 60 nitesh.may be it will long waiting.

      Delete
    2. i think it is not for quantity

      Delete
  21. thanks for uploading working code.
    its work into the localhost but not in, web server please help me to send message by using the server !

    ReplyDelete
    Replies
    1. yogesh it will works in all servers,check it any queries comment me

      Delete
    2. this is Error Shows in the domain name :
      500 - Internal server error.
      There is a problem with the resource you are looking for, and it cannot be displayed.

      Delete
  22. thank you so much
    itss working very well

    ReplyDelete
  23. thank you so much
    itss working very well

    ReplyDelete
  24. Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\sms\class.curl.php on line 59

    ReplyDelete
    Replies
    1. hi danish, i think your not enable the curl.php in php.ini file,check once, it will gets

      Delete
  25. hi i used the app and pressed send sms button but following response appeared in browser:

    Logged In SuccessfullyLogged in Successfully Sending SMS .. Msg:hello worldError in sending smsHTTP/1.1 302 Found Server: Apache-Coyote/1.1 Location: http://site21.way2sms.com/sendSMS.action Content-Length: 0 Date: Sun, 21 Feb 2016 08:23:26 GMT Connection: close HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=A03~17FA1EB82682203BC7DB61FCD7C807B7.w803; Path=/; HttpOnly Content-Type: text/html;charset=ISO-8859-1 Transfer-Encoding: chunked Content-Encoding: gzip Vary: Accept-Encoding Date: Sun, 21 Feb 2016 08:23:26 GMT Connection: close Error in sending message

    pls help

    ReplyDelete
    Replies
    1. i think may be server problem,otherwise clear the history in all browsers and try it. which server you are using

      Delete
    2. i think check the internet connection for your system i think it is slow so it is getting problem

      Delete
  26. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. i think curl.php problem in your program,enable the curl.php in php.ini file available in server ok

      Delete
  27. This comment has been removed by the author.

    ReplyDelete
  28. 1.Warning: include_once(class.curl.php) [function.include-once]: failed to open stream: No such file or directory in C:\wamp\www\WS\example.php on line 5

    2.Warning: include_once() [function.include]: Failed opening 'class.curl.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\WS\example.php on line 5

    3.Warning: include_once(class.sms.php) [function.include-once]: failed to open stream: No such file or directory in C:\wamp\www\WS\example.php on line 6

    4.Warning: include_once() [function.include]: Failed opening 'class.sms.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\WS\example.php on line 6

    5.atal error: Class 'sms' not found in C:\wamp\www\WS\example.php on line 9

    These 5 errors are shown when I Run the codes.What modification is needed to execute properly.

    ReplyDelete
    Replies
    1. i think may be u have to enable the curl.php in php.ini file, some of the above files are missing put all files in same folder and try

      Delete
  29. how can i use this code for lamp server.?..its not sending the message. I feel there is problem to login.
    plz help me through this..its part of my project to send sms.

    ReplyDelete
    Replies
    1. ok, where exactly problem in login,check it and post the errors and warnings

      Delete
    2. on saturday and sunday i will free, ok i will check on those days

      Delete
    3. this code worked fine in windows installed xampp server...as i checked in ubuntu installed lamp server(Apache2) the same code,its isn't working!
      And there is no error to debug too..I have no idea where its going wrong!

      Delete
    4. Sorry i also dont have idea on ubuntu installed servers

      Delete
  30. AWESOME SCRIPT BRO WORKING ON LOCAL SERVER AS WELL

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

    ReplyDelete
  32. Sir I got the following error

    PHP Error Message

    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/a6651060/public_html/class.curl.php on line 71

    Free Web Hosting
    Error Logging InHTTP/1.1 302 Found Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=A04~07DD32AA702BA2F70317758493DA75FC.w804; Path=/; HttpOnly Location: http://site24.way2sms.com/ebrdg.action;jsessionid=07DD32AA702BA2F70317758493DA75FC.w804?id=07DD32AA702BA2F70317758493DA75FC.w804 Content-Length: 0 Date: Sat, 28 May 2016 05:14:50 GMT Connection: close

    please help

    ReplyDelete
  33. which server you are using, disable the safe mode in that server

    ReplyDelete
    Replies
    1. Thank you sir. But, I don't know how to do that. I am using www.000webhost.com.
      Please guide me.

      Delete
    2. This comment has been removed by the author.

      Delete
  34. Error Logging InHTTP/1.1 302 Found Server: Apache-Coyote/1.1 Location: http://site24.way2sms.com/vem.action?id=0285E550FEDC9CF361F4D06C3CB1FB73.w810 Content-Length: 0 Date: Thu, 30 Jun 2016 09:29:39 GMT Connection: close HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/html;charset=ISO-8859-1 Transfer-Encoding: chunked Content-Encoding: gzip Vary: Accept-Encoding Date: Thu, 30 Jun 2016 09:29:39 GMT Connection: close

    ReplyDelete
  35. Call to undefined function curl_init() in C:\wamp\www\SMS2\class.curl.php on line 59

    ReplyDelete
  36. for some numbers it is going for some mobile numbers it is not going ? why ?

    ReplyDelete

  37. sir it showing the following error

    plz help me

    Error Logging InHTTP/1.1 302 Found Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=A05~14456DCF90070C9455C39EE7B417520E.w805; Path=/; HttpOnly Location: http://site24.way2sms.com/main.action;jsessionid=14456DCF90070C9455C39EE7B417520E.w805?Token=14456DCF90070C9455C39EE7B417520E.w805§ion=s Content-Length: 0 Date: Wed, 24 May 2017 06:06:04 GMT Connection: close HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/html;charset=UTF-8 Transfer-Encoding: chunked Content-Encoding: gzip Vary: Accept-Encoding Date: Wed, 24 May 2017 06:06:04 GMT Connection: close

    ReplyDelete
  38. it makes error getting domoian error what i do

    ReplyDelete
  39. Error Logging InHTTP/1.1 302 Found Server: Apache-Coyote/1.1 Location: http://site21.way2sms.com/main.action?Token=093CCF49C067EFFB4295EFDACBFBD1A5.w803§ion=s Content-Length: 0 Date: Tue, 23 Jan 2018 16:31:16 GMT Connection: close HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/html;charset=UTF-8 Transfer-Encoding: chunked Content-Encoding: gzip Vary: Accept-Encoding Date: Tue, 23 Jan 2018 16:31:16 GMT Connection: close

    ReplyDelete
  40. Try MsgClub SMS API is pretty simple . HTTP API is most popular API. It is allows you to Integrate SMS API PHP services into your own System/application for better functionality required by you.

    ReplyDelete
  41. Nice blog. MsgClub SMS API PHP send a text message directly from your own software, website or application which developed using php code is now gets possible. Your search for sending information from a reliable and robust application will get end here because MsgClub is helping api users.
    Bulk SMS API

    ReplyDelete