way2sms code in php

PHP Code for Sending free SMS through your way2sms account…

This article is about a Php code which is used to send SMS to your Indian mobile number through way2sms service provider. You will require an account with way2sms in order to use this code. Account registration at way2sms.com is free.

The code in the article is a php function which uses the cURL library. Before you run the code you need to ascertain that the cURL library is also installed along with Php.

The function takes four arguments (parameters). Those are $userID, $userPWD, $recerverNO & $message. Note that the arguments $userID and $recerverNO are redundant as both are the same in our case that is for Ways2SMS and represents your mobile number. $userPWD is the password for your Ways2SMS account and $message is the message that you want to send in the SMS.

The code checks for the existence of the cURL library. If not found, it stops the execution of the function and comes out. Else it proceeds by checking the length of the message. Ways2SMS has a limit of 140 characters for the maximum message size. So, if the message is longer than 140 characters, it is truncated, that is, only the first 140 characters of the message are used.

The location to store the cookies, the temporary file, and the user agent are defined. The URL to the login page is set along with login credentials and login button. The curl options are set for this URL and executed. The result is html page stored in $result variable. This html code is saved to the temporary file which was defined earlier.

The URL for signing out from Ways2SMS is fetched from temporary file previously created. The signing out URL can be located in the temporary file on a line which begins with “ window.location=”. Similarly the session id is also fetched from this temporary file which can located on a line which begins with “JSESSIONID”.

The URL to sent SMS is set along with the message credentials. The curl options are set for this URL and executed. The result is a html page giving the message of confirmation of the sending of the SMS which we do not save.

The URL to sign out is set, than the curl options are set for this URL and executed. The temporary files created are deleted and the function is closed.



<?php

function send_sms($userID$userPWD$recerverNO$message)
{
if (!
function_exists(‘curl_init’)) {
echo 
“Error : Curl library not installed”;
return 
FALSE;
}
$message_urlencode rawurlencode($message);
if (
strlen($message) > 140) {
$message substr($message0139);
}

$cookie_file_path “./cookie.txt”;
$temp_file        “./temporary.txt”;
$user_agent       “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36”;

// LOGIN TO WAY2SMS

$url        “http://site24.way2sms.com/content/Login1.action”;
$parameters = array(
“username” => $userID,
“password” => $userPWD,
“button” => “Login”
);

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_POSTcount($parameters));
curl_setopt($chCURLOPT_POSTFIELDS$parameters);
curl_setopt($chCURLOPT_HEADERTRUE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
curl_setopt($chCURLOPT_USERAGENT$user_agent);
curl_setopt($chCURLOPT_NOBODYFALSE);
$result curl_exec($ch);
curl_close($ch);

// SAVE LOGOUT URL

file_put_contents($temp_file$result);
$result     “”;
$logout_url “”;
$file       fopen($temp_file“r”);
$line       “”;
$cond       TRUE;
while (
$cond == TRUE) {
$line fgets($file);
if (
$line === FALSE) { // EOF
$cond FALSE;
} else {
$pos strpos($line‘ window.location=”‘);
if (
$pos === FALSE) {
$line “”;
} else { 
// URL FOUND
$cond       FALSE;
$logout_url substr($line, –25);
$logout_url substr($logout_url021);
}
}
}
fclose($file);

// SAVE SESSION ID

$file fopen($cookie_file_path“r”);
$line “”;
$cond TRUE;
while (
$cond == TRUE) {
$line fgets($file);
if (
$line === FALSE) { // EOF
$cond FALSE;
} else {
$pos strpos($line“JSESSIONID”);
if (
$pos === FALSE) {
$line “”;
} else { 
// SESSION ID FOUND
$cond FALSE;
$id   substr($line$pos 15);
}
}
}
fclose($file);

// SEND SMS

$url        “http://site24.way2sms.com/smstoss.action?Token=” $id;
$parameters = array(
“button” => “Send SMS”,
“mobile” => $recerverNO,
“message” => $message
);

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_POSTcount($parameters));
curl_setopt($chCURLOPT_POSTFIELDS$parameters);
curl_setopt($chCURLOPT_HEADERTRUE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
curl_setopt($chCURLOPT_USERAGENT$user_agent);
curl_setopt($chCURLOPT_NOBODYFALSE);
$result curl_exec($ch);
curl_close($ch);

// LOGOUT WAY2SMS

$url “site24.way2sms.com/” $logout_url;

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_HEADERTRUE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
curl_setopt($chCURLOPT_USERAGENT$user_agent);
curl_setopt($chCURLOPT_NOBODYFALSE);
$result curl_exec($ch);
curl_close($ch);

// DELETE TEMP FILES

unlink($cookie_file_path);
unlink($temp_file);

return TRUE;

}
?>
Please share the code so that everyone benefits.

Click on this link to download the code.

134 thoughts on “PHP Code for Sending free SMS through your way2sms account…

  1. This is very useful. I am getting message ‘Success sent’, but the message is not being sent. Please help me. The logout URL is stored and session id is stored but in the last part strpos() is not finding the quoted string.

  2. Data is saving into teproary file but cookie file is blank…….message is session is failed……..
    plz help me……….

    1. Data is saving into teproary file but cookie file is blank…….message is session is failed……..
      plz help me……….

    1. It is the signature added by the way2sms gateway. It is specific to your plan with way2sms. If you can negotiate with way2sms and get it removed for your account then yes.

  3. Sir Plz guid me all the steps of working on by one on mail, because i have a online server and i want to run this on that. But it continously say “Faliure”.
    Plz Sir Help Me B/c Its So Important For Me..

  4. I have go through your code but it is showing failure message.
    In Temporary.txt following script found, 400 Bad request, Your browser sent an invalid request.

    Please help me to solve the problem.

  5. Can I use the code in localhost?
    temperory.txt file is being created but not creating cookie.txt

    When i run the code it is telling Session Failed.

    Please help !!
    Pradeep

  6. I used same code and put correct username and password also. but unable to send message. Please suggest me where I am wrong?

    Thanks

  7. Hello,

    Thanks for the post.
    Its working fine and its very helpful to me.

    can we change the sms title and remove the app link from the end of message?

    Thanks .

  8. Great code! Totally works!

    Is there a way to remove the sender number and the promotional “Reply via way2sms.com. Now available on your mobile” text in the delivered message?

    Keep up the good work!

      1. @Sailesh, your web server is not configured properly. As the request to the way2sms server is send through the webserver not your browser.

  9. Hi,
    I have downloaded the code and tried,but I’m not able to get any message delivered. Can you please help me what login credentials i need to change ??
    and is temporary.txt and cookie.txt need to be created, if so what is to be written in those two files??

    1. Hi,

      The login credentials means your login id and password to connect to way2sms. if proper authorizations exists then the cookie and temporary files will be automatically created.

      1. thank you,
        but i have given the correct id and password, still I’m not getting any message and there is error saying cookie.txt and temporary.txt path is not found.

        1. That means you do not have authorizations to create files in the folder where you have placed your .php file. This could be because the “user” which php scripts use is different than the one you use for login in. Give authorizations to web (php) user to access the directory and the script will work.

          1. How do you know you have given authorization to your web user? Who is your web user? Please note the current issue is with the way your web server and web site is set up and is out of the scope of this article. Yet I am ready to help you if you help yourself. Please don’t make wage statements. Give the details for the first two questions.

          2. No, still the same warning temporary.txt and cookie.txt not found.
            Is there a way creating those files manually will work??

  10. Hi admin please send me working code to my maid id.. i copied this code and changed creadentials , i m trying to run in xampp server it showing ‘session fielded’ error

    1. please download the code and then change the login credentials. The cord works if the credentials supplied are correct. If you use the default credentials it definitely fails.

  11. Hi,
    Previously the above mentioned php code was running fine. But very recently the same code is not working. I have tried heart and soul to modify the php code so that it can send sms to a specific mobile number. But failed! may be the website way2sms has been changed. The above code can login and logout in the account successfully, but it fails to send sms. Please help me……..

    1. Hi Selvin
      I am trying sms sending code please give me which data are include bellow files
      $cookie_file_path = “./cookie.txt”;
      $temp_file = “./temporary.txt”;

      Please Reply

    2. Hi,
      I tried this code, but it wasn’t working for me, can you please specify what changes did you make other than your id and password for way2sms account.

  12. Hi Sir,

    When i run the code, the browser is just keep on loading and i dont see any thing happening. Please let me know if i need to change something in the code that will work for me other than userid, password, receiver no and message. I’m just trying this for past one month with referring almost all the sites but i’m not able to make it work. please help me.

  13. Hi
    I am using Easyphp with the php verison as 5.x. I am getting multiple warnings messages and is also getting a Syntax Error for the “id” field. I had changed the “error_reporting” also in the php.ini but its not helpng. Can you please help resolve the error

  14. I think that way2sms has changed their code because your code is fine till finding token and logouturl but while send sms if we echo the $result variable it is giving bad request HTTP no cache

  15. Notice: Undefined variable: id in C:\xampp\htdocs\myprojects\sms.php on line 101 I am getting this error.

    Please send me working code sir.. Urgent requirement is there..

    Thanks and Regards
    Praveen

  16. can we send more than 9 messages for at a time using group message in way2sms website…or can we code automatically send more messages on after the other

    1. @Siddartha, I have not tested the program from that perspective as I never had the intention of mass SMS while writing the original code.

  17. I have written the code with out syntax errors , I am logging in to my account through the code (i can see in last login details in the site ) & when i give
    >>>echo “result” .$result <<< in the // LOGIN TO WAY2SMS segment , the web browser displays way2sms site which is logged in page of my account.
    the error i get is "Undefined variable: id" , i.e is in the first line of // SEND SMS section please help. Please sir.

        1. Dear Sir ,
          The code did not work 🙁 , i have sent you the proof in mail (screenshots ) , please help me sir.

          Thank you.

    1. @Nithin thanks for trying and giving the details. Please note that the undefined variable id is a syntax error. This is either because your php is configured to be very strict E_STRICT i.e. it is compulsory to define the variable before use or in the line number shown by your error there is an invisible character that has been copied along during copy and paste from this website. In the later case the solutions is either to use an editor which can display all the special characters and you manually correct them or manually rewrite the code. For the former case you need to consult the system administrator.

        1. Sir , is there any way i could send you my index.php & filr of your php code ? shall i upload in 4shared ? can you check it , please sir.

    1. @Nishant, we are currently not offering the ready to use code. So, to test the code you will have to rewrite the above code and call it from your php script. We will definitely help you once you have written the code without any syntax errors and yet you are not getting the desired result. BEST OF LUCK.

    1. Hi, DEBOTTAM DAS

      Plz send me the PHP Code for Sending free SMS through way2sms account gateway Code file this is not working.i tray many time, If u give me i will save my office and i give many many thanks.
      This is my M-ID:- kuberdakua@gmail.com

      merry christmas.

    1. $userID, $userPWD are the variables which need to contain the username and password for logging into you way2sms account. These are passed to the function as parameters. Please refer PHP manual for further understanding of how to call a function.

  18. i am getting many error like Invalid Arguments on line 44,47,51,65,90,130 and undefined variable at line 44,47,51,65,90,130…Can you help me out

    1. You can simply retype the code without any typing errors and it will work. If by chance it does work than i will definitely hep you. You can see that none of the previous commentators had any problem with the code else they would have commented again.

  19. Getting Error – ” Parse Error : Syntax Error , unexpected ‘<' in E:\SMSSEND.PHP ON LINE 21.

    Line 21 code is "$user_agent = “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36″; "

    Kindly help me.

    1. When you copy and paste the code, some special characters also get copied. So please retype the code and try. Whether works or not do comment the response. I will definitely help if it does not work.

  20. Thanks for the code …. gave a detailed idea of its working but the code is not working for me …. I created account yesterday …. I’m in need of it …. plz tell me which OS you used along with the version of php & apache …. Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *