The following class help you to send sms via way2sms gateway.
Features of this class:
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 ($curl, CURLOPT_URL, "http://site".$autobalancer.".way2sms.com/Login1.action");
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, "username=" . $this->uid . "&password=" . $this->pwd);
curl_setopt ($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt ($curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_MAXREDIRS, 20);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_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 ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_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($curl, CURLINFO_EFFECTIVE_URL), "main.action");
if ($pos === "FALSE" || $pos == 0 || $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($curl, CURLINFO_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 ($curl, CURLOPT_REFERER, $refurl);
curl_setopt ($curl, CURLOPT_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($curl, CURLINFO_EFFECTIVE_URL);
}
// Send SMS
curl_setopt ($curl, CURLOPT_URL, "http://site".$autobalancer.".way2sms.com/jsp/stp2p.action");
curl_setopt ($curl, CURLOPT_REFERER, $refurl_instantsms);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_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 ($curl, CURLOPT_URL, "http://site".$autobalancer.".way2sms.com/jsp/logout.jsp");
curl_setopt ($curl, CURLOPT_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);
$result=$obj->sendSMS('mobile_number','message');
echo implode('<br >',$result);
click here for demo.
Thanxx A lottttttttttttttttttttttttt ...............
ReplyDelete: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in how solve this
ReplyDeletehttp://stackoverflow.com/questions/3349280/php-replacement-for-safe-mode
DeleteThis should helps you.
Send Way 2 sms to property owners from The best Indian property website
ReplyDeleteMessage Sucessfully send to : 9714158984.
ReplyDeletebut i will not send message on mobile what is problem of this code plz say me quickly
Can't get your question chirag!
DeleteHey i get this error
ReplyDeleteNotice: Undefined offset: 0 in wamp\www\way2smsapimaster\way2sms.php on line 72
Hi Dhaval Desai, it should be because of empty $pharr online 72.
DeleteMake sure that you are passing a valid mobile number.
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 :
DeleteArray ( [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
API updated please try updated API.
Deletemessage sent successfully. But No message sent to receiver mobile
ReplyDeleteThanks for your notification!
DeleteI guess , there should some in way2sms side!
Let me update the correct code soon!
API updated please try updated API.
DeleteIt shows message that Message Sucessfully send to : 9595461232.
ReplyDeleteBut i didn't get any message.
Plz help
Again way2sms team made some change in there code.
DeleteSure soon let me update the correct one !
Now I'm on another work!
API updated try now!
DeleteThanks!
Hi, your demo is working....but when i uploaded into my site and tried i get "Login Faild"
ReplyDeleteHi , It should work!! make sure that you have entered a correct password? and you server cURL enabled one ?
DeleteHi JJ,
ReplyDeletei 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
Hi sri,
Deletewhat was the error you got?
Hi JJ
Deleteits working now....thank you.
how to run the above code....
ReplyDeletewhen i m running the code in php it displays nothing on the screen......
please tell me how to run the above code in step by step procedure........
ReplyDeleteHi,
DeleteCreate 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);
thanks for you way2sms script its very useful to me
ReplyDeletethanks for giving script but,it doesn't work send multiple contacts way2sms
ReplyDeleteit's given error can u help me?
Yes, What I have to do?
Deleteerror in continue; line in if statement.
ReplyDeleteif you remove upper foreach loop then you must remove that continue statement too..
Yes, you are right!
DeleteThanks for the tip!
Hi JJ,
ReplyDeleteI 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
Hi Sri,
DeleteDid you got any error message?
I'm not familiar with flash sri :) Is that PHP code returns any error message??
Thanks.
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.
Deletein ur demo, code saying message sent successfully, but we didnt get message. then I tried sending through way2sms.com, i got the message.
ReplyDeleteCan you verify the script, may be its not working now.
Ok I will.
DeleteThanks for your notification!
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.
DeleteSure!
DeleteHey how to send the sms..
ReplyDeletePlease show step by step procedure..
See this comment
Deletehttp://justtrythis.blogspot.com/2012/06/way2sms-php-api.html?showComment=1361362784463#c8591363861174309267
Hi,
ReplyDeletei 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?
Sure it's because some change on way2sms side, Need to update the script.
DeleteGive me some time, Let me update it soon! I'm on another work now!
Thanks.
In your demo it shows Login faild
ReplyDeleteNo Warning But Login Failed
ReplyDeleteNotice: Undefined offset: 0 in /opt/lampp/htdocs/w2s.php on line 55
ReplyDeleteLogin Faild
Please Help Me....I'm in urgent need....
i am unable to login with ur code ..it says login failed..i tried it on my behalf and ur demo site too
ReplyDelete.plz help me
It shows "Login Faild". Username, Password are ok with direct site login. plz help me.
ReplyDeleteHi to all,
ReplyDeleteAgain 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.
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.
ReplyDeleteMessage - Login Faild
Please help me out..................
ReplyDeleteNotice: Undefined offset: 0 in C:\wamp\www\smsf.php on line 55
the line is : $token = $match[1][0];
Message contentStatus1:
DeleteNotice: Undefined offset: 0 in C:\xampp\htdocs\mmarket\source\way2sms.php on line 55
Login Faild
Notice: Undefined offset: 0 in C:\xampp\htdocs\sms\sendsms.php on line 57
ReplyDeleteLogin Faild
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
ReplyDeleteI got the Login failed in the ur demo.
ReplyDeleteLogin failed.
ReplyDeletePlz help.
Notice: Undefined offset: 0 in line no 59
ReplyDelete$token = $match[1][0];
pls help me
Message Sucessfully send to : +919688416447....
ReplyDeletebut 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
Hello,
ReplyDeleteYour demo link is says Login Failed. But I provide right username and password for testing.
synte error at declaration of class
ReplyDeletelogin failed
ReplyDeletelogin failed
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteFor 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.
ReplyDeleteOur 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