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.
{
if (!function_exists('curl_init')) {
echo "Error : Curl library not installed";
return FALSE;
}
$message_urlencode = rawurlencode($message);
if (strlen($message) > 140) {
$message = substr($message, 0, 139);
}
$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($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($parameters));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
$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_url, 0, 21);
}
}
}
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($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($parameters));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
$result = curl_exec($ch);
curl_close($ch);
// LOGOUT WAY2SMS
$url = “site24.way2sms.com/” . $logout_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
$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.
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
Please update me on the error you are receiving (if any).
Trying..
Not working
plz talk with me and solve error
It does work. send me your email id and i will send you the same code but without the html characters.
it not working.
please send me steps and code.
Did the code I sent work for you?
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.
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.
Please Send me the Working Code in my mail id mail2souravdas25@gmail.com.
Thanks.
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.
Yes bro i done it.Thanks a lot…
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
sure. Did u retype the code?
Do we need to have anything in the Cookie and temporary text files or it should be empty
The cookie file is created, used and deleted during program execution. So there is no question of having anything in it.
Where to enter our username, password, email etc?
$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.
I am in trouble. Code still not working.Please send working code on my id: dvrsh.patel11@gmail.com
Thanks in advance.
Unfortunately, I do not like giving the code files. (may be harsh but true). Please retype the code cautiously.
Thx a lot bro, Code worked for me (thumbsup).
please sent working script
please sent working script…my mail id is
spmuthu21@gmail.com
The code is still working, only thing you need to cautiously do is retype the code.
Thank you very much. Your program to send sms is working perfectly. It is rocking.
By DEBOTTAM DAS
Thanks for reply, glad it helped you. Please spread the word.
Wishes,
Gajanand Patell (@GajanandSEO on twitter)
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.
May you please send me the whole working code with html in my mail id nishant.nikky@gmail.com.
@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.
Ok.Thanks 🙂
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.
@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.
i corrected that error !!! now the code has no errors , but yet message is not delivered !
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.
Dear sir , Please reply to my mail. Thank you.
@Nithin please do update me if the program is working or not.
Dear Sir ,
The code did not work 🙁 , i have sent you the proof in mail (screenshots ) , please help me sir.
Thank you.
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
@Siddartha, I have not tested the program from that perspective as I never had the intention of mass SMS while writing the original code.
I am getting this error… Notice: Undefined variable: id in C:\xampp\htdocs\myprojects\sms.php on line 101..
Please any one send me working code for send me sms to kpkproject1@gmail.com
Thanks in advance
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
@praveen you are getting those errors because you have copied and paste the code and note rewritten it.
Im not getting errors,its not running also.Can you help me out…
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
bad request
400 Bad request
Your browser sent an invalid request.
what went wrong ?
Sorry solved the probelm
The code works 🙂
Have to trim the token id to remove extra space
Dude Can u send me the working code please .. its argent
prashantkumarkb@gmail.com
I have tried the code, it is giving Invalid Request.Please try again..
Can you send me working code, please my email id is
shaileshpatki2012@gmail.com
Code is working …..!
Sir… i m getting the error message that id is not defined
can u please send me the working code.. to my email ID ..thanks u sir
prashantkumarkb@gmail.com
sir…
thank you for the script .. but em getting only this “session failed”
Send That Code Me
A hyper link to download the code is there at the end of the article itself.
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
Dear sir,
The code did not work,i got an error “Undefined variable: id” Can anyone please send me the working script, my mail id given below.
visakhmm88@gmail.com
Thanks you
Thanks, Its work 4 me, but locally could not work
hi, the code is not working for me. no error doesnt recieve msg.
riyaz.developer@gmail.com
plz send me working code i an getting ID error
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.
This code is not working, can you send me working code to my email : bytespool2014@gmail.com
Hi every one,
The code download link is now added at the end of the article. Please update the account details and run the code.
Regards.
I tried the code, it is giving failure and temperary.txt files giving 400 Bad request Your browser sent an invalid request.
Please help me to solve the problem, my email id is shaileshpatki2012@gmail.com.
Its a working code. I guess you need to host this code onto your server to work
You need to give the correct way2sms login credentials as the credentails provided in the code are dummy. Hence it is not a 100% working code.
should I create a form to process this script or I have to join the parameters in URL like GET services?
I am in trouble. Code still not working.Please send working code on my id: tamizh288@gmail.com
Thanks in advance.
session failed is the output displayed
if(!isset($id))
{
}
is executed can you help with me…..
Hi I have use our code but this code working plz help me
plese send me a working code
it show error seesion failed
Wow that’s working fine.. thanks a lot
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.
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
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……..
Is this script runs on internet web server as it runs on localhost…………….
code is not working.Please send working code on my email id: amitsharma1676@gmail.com
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.
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
please say about solution this error I get same error
session fielded
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??
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.
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.
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.
No, still the same warning temporary.txt and cookie.txt not found.
Is there a way creating those files manually will work??
Please help me!!!
It is not working even after giving the authorization to the web user.
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.
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!
May be that’s possible if you buy a plan from way2sms. Hope this helps.
Dear Sandeep,
I have tried the code but getting 400 Bad request, Your browser sent an invalid request.
Can you send me working code. My email id is
shaileshpatki2012@gmail.com
@Sailesh, your web server is not configured properly. As the request to the way2sms server is send through the webserver not your browser.
can you tell me how it is working beacuse when i run this send messege is display but we don’t receive any messege please tell me in detail on kumar.harsh492@gmail.com
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 .
WOULD YOU PLEASE SEND ME THE WORKING CODE
this code is still works like a charm,
Thank you mate
I used same code and put correct username and password also. but unable to send message. Please suggest me where I am wrong?
Thanks
Please throw some light on the error you are receiving.
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
check if cookie is enabled on your local server
session failed.. help me
Please provide more details about your web server setup.
Not working.
Can you mail working code to me ??
Hii, please send me working code on my email “amitsharma16761676@gmail.com”
This is not proper work so i’m requast plz sent me complate details.
Thanks sir ji
Please setup your webserver environment correctly. It will definitely work.
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.
its not work….help me…!
is there any sms limit in way2sms per day?
30
Display success message but, message not delivering…!?
session failed is the output.
I am not receiving any sms. I have tried using my waysms account but I am not receiving any sms. plz help me
Hi
i have proble while run my code it alwayes tell session fail please help me
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..
hello…. its not working ! can you please send me the working source code to ” sk@benztechnologies.ml ” ?? thank you ….
What are these?
$cookie_file_path = “./cookie.txt”;
$temp_file = “./temporary.txt”;
These are temporary file names with their locations used during execution
Hi, sir i am facing problem “Login Failed” so help me
Please check your username and password. Also check it no additional characters such as space or tab are going along.
Please approve your email id from your way2sms account, Thanks …………
Can we remove “Sent via WAY2SMS.COM” from SMS . If we can please let me know and i appreciate your work, Thank you………..
Please send it at amitagrawal2050@gmail.com, if there is any solution for the same.
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.
Sir,
My way2sms account does not work.so sir please helpme. It shows in the message box that undefined.
sir,
My way2sms not works. In message box shown that undefined, so please help me, my cell no-9438372856
Hi can i change sender id in this script. Pls reply me at bharatrawat000@gmail.com.
Sender id is same same as your username for way2sms hence you can create a different account at way2sms portal and use it with this code to achieve the same.
Data is saving into teproary file but cookie file is blank…….message is session is failed……..
plz help me……….
Data is saving into teproary file but cookie file is blank…….message is session is failed……..
plz help me……….
hey you can download this to get the way2sms API
https://github.com/kingster/Way2SMS-API
is this code will work in localhost
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.
hello sir/ madam
how to send schedule messages from php using way2sms api
I’m trying, but for verification of the co-registration is not coming.
it not working.
please send me steps and code.
Please help me…
Hi ,i cant send more than 29 sms per day?
it is default. you can only send 29 free sms per day
sir this will not working, error for
Session Failed
I work on localhost
Please provide only web api link with its parameters like ID,pass,ph no, message
So that I can use it with c# language
Thank you
jsessionid not geting in cookie.txt
Where is the html of this php code? Please send on my email..
sir please can you send me step wise run able code
please sir can you me send me stepwise runable code
Hii I am beginner. so help me how is this code work in php