Monday, 27 February 2012

PHP Code for Sending free SMS through your way2sms account... :)

Guys here is code written in php for sending sms through your way2sms account. (UPDATED 13/06/2012)

This Code works fine for your small application where you want to send message through your website. Only thing you needed is an account on WAY2SMS . First create an account, then insert this code in your website and pass 4 argument to the function and just sit back and relax :), this code will do automatically everything for you..enjoy :)

You can download complete source in ZIP :
http://andromeda.nitc.ac.in/~ritesh/SmsApi/SmsApi.zip

Some Points to Remember...

  1. Some lines are truncated in this webpage so keep track of  double quote("") and new lines.
  2. You have to create a normal text file with the name cookie.txt and place it your local host or any folder and set absolute location of cookie file to $cookie_file_path_location variable. like this  $cookie_file_path_location ="/var/www/sms/cookie.txt";
    find Absolute path using php function realpath("your_cookie_fine_name");
  3. Cookie file must have full READ and WRITE permission. Give 777 permission in Linux and full read write permission in windows.
  4. Message length must be less than or equal to 140 character.
  5. By repeating Code for sending SMS in loop, you can send same message for many time without login and logout.
  6. Do not include '0' or '+91' before mobile number.
  7. Before using this code make sure you have installed PHP CURL libraries libcurl. It is necessary for executing this script. 


//Setting up variables 
$userID="9876543210"; //10 digit, way2sms id, from which admin will send SMS
$userPWD="123456"; // password for user id
$recerverNO="9876543210"; // recever's Number, who is going to receive sms
$message="Anything that you want to send but less than140 character"



******************** CODE START HERE ********************


function send_sms($userID,$userPWD,$recerverNO,$message)
{
     if(strlen($message)>140) // check for message length
         {echo "Error : Message length exceeds 140 characters" ; exit(); }
     if (!function_exists('curl_init')) // check for curl library installation
         {echo "Error : Curl library not installed";  exit(); }

     $message_urlencode=rawurlencode($message);
      // message converted into URL encoded form
     $cookie_file_path ="/var/www/oose/cookie.txt";
     // Cookie file location in your machine with full read and write permission

//START OF Code for getting sessionid
        $url="http://site5.way2sms.com/content/index.html";
        $header_array[0] = "GET /content/index.html HTTP/1.1";
        $header_array[1]= "Host: site5.way2sms.com";
        $header_array[2]= "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1";
        $header_array[3]= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        $header_array[4]= "Accept-Language: en-us,en;q=0.5";
        $header_array[5]= "Accept-Encoding: gzip,deflate";
        $header_array[6]= "DNT: 1";
        $header_array[7] = "Connection: keep-alive";
        $ch = curl_init();   //initialise the curl variable
        curl_setopt($ch, CURLOPT_URL,$url);
        //set curl URL for crawling
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array); 
        //set the header for http request to URL 
        curl_setopt($ch, CURLOPT_REFERER, $reffer);  
         //set reffer url means it shows from where the request is originated.
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
         //it means after crawling data will return
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        // store the return cookie data in cookie file 
        $result = curl_exec ($ch); // Execute the curl function 
        curl_close ($ch);
//END OF Code for getting sessionid

//START OF Code for automatic login and storing cookies
        $post_data = "username=".$userID."&password=".$userPWD."&button=Login";
        $url = "http://site5.way2sms.com/Login1.action";
        $header_array[0]="POST /Login1.action HTTP/1.1";
        $header_array[1]="Host: site5.way2sms.com";
        $header_array[2]="User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1";
        $header_array[3]="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        $header_array[4]="Accept-Language: en-us,en;q=0.5";
        $header_array[5]="Accept-Encoding: gzip, deflate";
        $header_array[6]="DNT: 1";
        $header_array[7]="Connection: keep-alive";
        $header_array[8]="Content-Type: application/x-www-form-urlencoded";
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        curl_setopt($ch,CURLOPT_REFERER,"http://site5.way2sms.com/content/index.html");
        $content = curl_exec( $ch );
        $response = curl_getinfo( $ch );
        curl_close ($ch);
//END OF Code for automatic login  and storing cookies

// START OF Code is  getting way2sms unique user ID
        $url = "http://site5.way2sms.com/jsp/InstantSMS.jsp";
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        $content = curl_exec($ch);
        curl_close ($ch);
        $regex = '/input type="hidden" name="Action" id="Action" value="(.*)"/';
        preg_match($regex,$content,$match);
        $userID = $match[1];
// END OF Code for getting way2sms unique user ID

// START OF Code for sending SMS to Recever
        $post_data = "HiddenAction=instantsms&catnamedis=Birthday&Action=".$userID."&chkall=on&MobNo=".$recerverNO."&textArea=".$message_urlencode;
        $url = "http://site5.way2sms.com/quicksms.action";
        $header_array[0]="POST /quicksms.action HTTP/1.1";
        $header_array[1]="Host: site5.way2sms.com";
        $header_array[2]="User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1";
        $header_array[3]="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        $header_array[4]="Accept-Language: en-us,en;q=0.5";
        $header_array[5]="Accept-Encoding: gzip, deflate";
        $header_array[6]="DNT: 1";
        $header_array[7]="Connection: keep-alive";
        $header_array[8]="Content-Type: application/x-www-form-urlencoded";
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
   curl_setopt($ch,CURLOPT_REFERER,"Referer: http://site5.way2sms.com/jsp/InstantSMS.jsp");
        $content = curl_exec( $ch );
        $response = curl_getinfo( $ch );
        curl_close ($ch);
// END OF Code for sending SMS to Recever

//START OF Code for automatic logout
        $url = "http://site5.way2sms.com/jsp/logout.jsp";
        $header_array[0]="GET /jsp/logout.jsp HTTP/1.1";
        $header_array[1]="Host: site5.way2sms.com";
        $header_array[2]="User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1";
        $header_array[3]="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        $header_array[4]="Accept-Language: en-us,en;q=0.5";
        $header_array[5]="Accept-Encoding: gzip, deflate";
        $header_array[6]="DNT: 1";
        $header_array[7]="Connection: keep-alive";
        $cookie_file_path ="/var/www/oose/cookie.txt";
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        curl_setopt($ch,CURLOPT_REFERER,"Referer: http://site5.way2sms.com/jsp/InstantSMS.jsp");
        $content = curl_exec( $ch );
        $response = curl_getinfo( $ch );
        curl_close ($ch);
//END OF Code for automatic logout

}// end function send_sms



******************** CODE END HERE ********************

69 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. It's gr8 job man... It's working very nice. Thanks for your efforts.

    ReplyDelete
    Replies
    1. Please send me the working code to selvarajcse59@gmail.com

      Delete
    2. dude plz send workind code of sms through way2sms. i have work on this.. please

      Delete
  3. is this legal to use this .... i mean way2sms won't have objection is using there service like this... I have to use this code in my wesite(organisation website).

    so please tell me that can i use this code legaly...
    please do reply

    ReplyDelete
  4. thanx a lot....:) i'm getting sms's...is there any idea to remove userid phone number displaying at the beginning of the text messages???

    ReplyDelete
    Replies
    1. can u send me the working code. to ev.srinivas@gmail.com

      Delete
    2. can u plz send me the working code.

      My id is
      ganeshrc35@gmail.com

      Delete
    3. can you send the working code in to darsa1990@gmail.com

      Delete
    4. plz send the working copy to prince.ella@gmail.com

      Delete
    5. Plz send the working code to sandeepg84@gmail.com

      Delete
    6. can u please send me the working code to ravikiranbc1989@gmail.com

      Delete
    7. can u please send me the working code to selvarajcse59@gmail.com

      Delete
    8. hai can u please send me working code to my mail id:
      janardhan.559@gmail.com

      plz.......

      thanks in advance

      Delete
  5. i am getting an error-"Notice: Undefined variable: reffer"
    and i am not getting sms all the time i send..plz help :(

    ReplyDelete
  6. HI,Thanks for sharing code.
    but iam getting errors like

    Notice: Undefined variable: reffer in location : curl_setopt($ch, CURLOPT_REFERER, $reffer);

    Notice: Undefined offset: 1 in location :
    $userID = $match[1];

    pls help me to solv this.

    ReplyDelete
    Replies
    1. use $reffer="http://site5.way2sms.com/jsp/InstantSMS.jsp";
      and i used the version downloaded in zip file so "Notice: Undefined offset: 1 in location : $userID = $match[1];" this error didn't came.....

      Delete
    2. i used the same but still i am getting the error "Notice: Undefined offset: 1 in location : $userID = $match[1];" and also the message is not sending pls any one help me

      Delete
  7. Great work !! thank you for sharing code :)

    ReplyDelete
  8. greate Work..But On the live Server i did not get any msg is it Cause eroor on live server in my local host it working ?

    ReplyDelete
    Replies
    1. can u please send me the working code to ravikiranbc1989@gmail.com

      Delete
  9. this api works fine, THANKS A LOT..

    ReplyDelete
  10. can any one tel me the detail process of configuring this php.bcaz i've tried many times ,but my msgs are not delivered

    ReplyDelete
  11. I got error at this line
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    but your code really worked...Thanx mate

    ReplyDelete
  12. hi,
    Its not working for me,
    neither in localhost nor in live server,
    Any suggestion from you will be helpful,
    Thanks

    ReplyDelete
    Replies
    1. Parse error: syntax error, unexpected T_FUNCTION in C:\wamp\www\makemyjainshaadi.com\sendsms.php on line 10
      this the error in local

      Delete
  13. Hi,
    Can any of you give me your proper code (whose application is working).
    thanks

    ReplyDelete
  14. Hi, I modified the code to send messages to multiple contacts.
    It works fine for up to 10 contacts but beyond that I get the error "Fatal error: Maximum execution time of 30 seconds exceeded". Can you make suggestions on how I can rectify this, if possible. Thanks!

    ReplyDelete
    Replies
    1. Hi Aldon,

      Can you please help me I need this type of script which will send multiple contacts,I have tried but it's not working.
      I am working for the noble cause i.e NGO website (http://humanrightsngo.com/).

      Thanks
      Pawan

      Delete
    2. Hi Aldon,

      Can you please help me, I need this type of script which will send a SMS to atleast one contact,I have tried but it's not working...

      I will use ur script in Engineering Final year project.

      Please......

      Thanks and Regards
      Ramesh Sowpati

      Delete
  15. 'Curl library not installed', this error is coming how can it be solved, pls anyone can reply.



    Thanks in advance

    ReplyDelete
  16. Can we run it on Localhost connected with internet
    coz i m trying & nt working
    plz help

    ReplyDelete
  17. I am not getting error after integrating the code,but i am not able send sms .in my cookiesfile.txt it logs this error.
    site5.way2sms.com FALSE / FALSE 0 JSESSIONID A01~373E22D0B226307EA18D4FC3010D3B42.w801.
    Please help me to resolve this issues.

    Thnaks,
    Rabindra

    ReplyDelete
    Replies
    1. I am also getting same prb if solved means plz let me know!!!!!!!!

      Delete
  18. hey.. i am getting error "Undefined offset: 1.." at line number 75 :p
    anyone please tell me why i am seeing it??

    ReplyDelete
  19. Replies
    1. can u please send me the working code to selvarajcse59@gmail.com

      Delete
  20. Notice: Undefined offset: 1 in /opt/lampp/htdocs/send_sms.php

    ReplyDelete
  21. I didnt get sms and also any error msg can tell wat is the prb.......?

    ReplyDelete
  22. use $reffer="http://site5.way2sms.com/jsp/InstantSMS.jsp";
    and i used the version downloaded in zip file so "Notice: Undefined offset: 1 in location : $userID = $match[1];" this error didn't came.....

    ReplyDelete
    Replies
    1. Can u pls put up the lines that contain variable $match in the zip file version?? give line numbers also.

      Delete
    2. hiii...I've used that $reffer...but still I'm getting offset error...Did u solve this??

      Delete
  23. Notice: Undefined offset: 1 in location :
    $userID = $match[1];
    finding error ..
    can any one tell me how to do variable $match in the zip file version

    ReplyDelete
    Replies
    1. U solved the error? If so, can u pls tell me wat did u do?

      Delete
  24. The zip file doesn't exist. can anyone re-upload or give the correct link for this file?

    ReplyDelete
  25. Looks like the site is changed, so it wont work anymore!

    ReplyDelete
  26. Notice: Undefined offset: 1 in location :
    $userID = $match[1];
    finding error ..
    can any one tell me how to do variable $match in the zip file version

    ReplyDelete
  27. http://site5.way2sms.com/jsp/InstantSMS.js Give a 404 page now ?

    ReplyDelete
  28. Undefined offset: 1 in location :
    $userID = $match[1];
    finding error ..
    can any one tell me how to do variable $match in the zip file version

    ReplyDelete
  29. there is no error but sms never to receive

    ReplyDelete
  30. Undefined offset: 1 in location :
    $userID = $match[1];
    finding error ..
    any one tell me how to do variable $match in the zip file version

    ReplyDelete
  31. I tried a lot i was uable to sort out. Please do send me a altered code to my mail id panpinky2@gmail.com.
    Please help me regarding this its urgent...
    Solution will be appreciated..

    ReplyDelete
  32. Undefined offset: 1 in C:\wamp\www\smser\sms.php on line 88

    Undefined variable: reffer in C:\wamp\www\smser\sms.php on line 37

    please provide the correct code..

    dhrubojyoti_das@yahoo.in

    ReplyDelete
  33. plz upload correct code

    ReplyDelete
  34. pleas tell me how to save all the code in single page or separate for each code section mentioned here

    ReplyDelete
  35. I am not Able to Download your "SmsApi/SmsApi.zip" file... it shows 'Page not found' error. Please give a live link to this file. I want to download this file.

    ReplyDelete
  36. Is new working code available ?

    ReplyDelete
  37. preg_match($regex,$content,$match);
    $userID = $match[1];//Here Showing this error
    Undefined offset: 1

    ReplyDelete
  38. i am getting a http_code=404 in this
    url:'http://site5.way2sms.com/jsp/InstantSMS.jsp'

    This is the response text
    *url -> http://site5.way2sms.com/jsp/InstantSMS.jsp
    content_type ->
    http_code -> 404
    header_size -> 137
    request_size -> 136
    filetime -> -1
    ssl_verify_result -> 0
    redirect_count -> 0
    total_time -> 0.506365
    namelookup_time -> 2.4E-5
    connect_time -> 0.252878
    pretransfer_time -> 0.252933
    size_upload -> 0
    size_download -> 0
    speed_download -> 0
    speed_upload -> 0
    download_content_length -> -1
    upload_content_length -> 0
    starttransfer_time -> 0.50634
    redirect_time -> 0
    certinfo -> Array
    redirect_url ->

    ReplyDelete
  39. its showing following error
    Could not connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known.

    though i have taken care of every steps

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

    ReplyDelete
  41. http://andromeda.nitc.ac.in/~ritesh/SmsApi/SmsApi.zip This link not working,how can i download the zip file

    ReplyDelete
  42. can u please send me the working code to ravi.c269759@gmail.com
    thanks in advance

    ReplyDelete
  43. HI ,
    I want send sms to group of number from sdcard, those are on whatsapp.
    if this feasible or not from my android phone, plz let me know how ..
    Some coding hint want.
    Any one having some clue plz share

    ReplyDelete
  44. CAn any one send working code plz.....

    ReplyDelete
  45. sms to multiple user at same time using php...any have code please send me ..
    Thanks

    ReplyDelete
  46. plz send me working code. This code not working in my system. It didn't any error messages. plz send me working code to email kalyan.tmk@gmail.com

    ReplyDelete
  47. will you please send me working code on my id that will be very help full
    sonawane.kuldeep@gmail.com this is my email id
    thanks by that way.

    ReplyDelete