September 20, 2012 / IST / Language, Web Development.


Hi Friends,
Here I am sharing PHP code to send SMS using gateway of way2sms.com or 160by2.com. First, you have to make account on Way2sms.com or 160by2.com.

Now follow bellow steps:

Step 1: Make a separate PHP file named send.php and copy and paste bellow code. Setup SMS gateway and username, password, sendto number and message.

<?php
//Use gateway of way2sms.com or 160by2.com
error_reporting(E_ALL);
ob_implicit_flush(true);

//If you get Fatal error: Call to undefined function curl_init() , Then you need to enable the curl extension in php.ini

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

$smsapp=new sms();
$smsapp->setGateway('160BY2'); // you can set gateway to be '160by2' to use your 160by2 account or way2sms to use your way2sms.com account;

$mobile_no = ''; //Mobile no (Username)
$pass = ''; //Password
$send_to_no = ''; //Your friend's mobile no
$msg = 'Hi Dear, How r u';

echo "Logging in  ... ";
$smsapp->login($mobile_no,$pass);

echo "Sending SMS ... ";
$result=$smsapp->send($send_to_no,$msg);

if($result=='true')
{
	echo "Message sent";
}
else
{	
	echo "Error encountered : ".$smsapp->getLastError();
}

?>

Step 2: Make a PHP file named class.curl.php and copy paste bellow code:

<?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;
}
}

?>

Step 3 : Make a PHP file named class.sms.php and copy paste bellow code.

<?php
class sms
{
	var $username;
	var $password;
	var $curl;
	var $server;
	var $loginDone;
	var $debugMode;
	var $data;
	var $error;
	public function __construct()
	{
		$this->curl=new cURL();
	//	$this->curl->setProxy("");
		$this->loginDone=false;
		$this->debugMode=false;
		$this->data=array();
	}
	public function setGateway($serverName)
	{
		switch($serverName)
		{
			case '160by2':
			$this->server='160by2';
			break;

			case 'way2sms':
			$this->server='way2sms';
			break;

			case 'airtel':
			$this->server='airtel';
			break;

			default :
			$this->server='160by2';

		}
	}
	public function login($username,$password)
	{
		$server=$this->server;

		call_user_func(array($this,"login_$server"),$username,$password);
		$this->loginDone=true;

	}
	public function send($number,$msg)
	{
		$server=$this->server;
		if($this->loginDone)
		return call_user_func(array($this,"send_$server"),$number,$msg);
		else
		{
			echo "<h2>Please login first before sending SMS</h2>";
		}

	}
	private function login_way2sms($username,$password)
	{
		$out=($this->curl->post("http://www.way2sms.com","1=1"));
		$pattern="/Location:(.+?)\n/";
		preg_match($pattern,$out,$matches);
		$domain=trim($matches[1]);

		$this->data['domain']=$domain;

		$out= $this->curl->post("{$domain}auth.cl","username=$username&password=$password&Submit=Sign+in");

		$pattern="/Location:(.+?)\n/";
		preg_match($pattern,$out,$matches);
		$referer=trim($matches[1]);
		$this->data['referer']=$referer;

	}

	private function send_way2sms($number,$msg)
	{
		$domain=$this->data['domain'];
		$html=$this->curl->post("{$domain}jsp/InstantSMS.jsp?val=0","1=1",$this->data['referer']);
		if($this->debugMode)
		{
		echo "<h2>After logging in, the HTML returned by server is</h2>";
		echo $html;
		}

		$pattern = '/name="Action".+?value="(.*)"/';
		preg_match($pattern, $html, $matches);

		$custfrom=$matches[1];
		$msg=urlencode($msg);
		$html=$this->curl->post("{$domain}FirstServletsms?custid=","custid=undefined&HiddenAction=instantsms&Action={$custfrom}&login=&pass=&MobNo=$number&textArea=$msg");
		$pattern = '/class="style1">(.+?)<\/span>/';
		preg_match($pattern, $html, $matches);
		$out=($matches[1]);

		if(!preg_match("/successfully/",$out))
		{
		$this->setError($out);
		return false;
		}
		else
		{
		return true;
		$this->setError("No errors");
		}

	}
	public function getLastError()
	{
		return $this->error;

	}
	private function setError($error)
	{
		$this->error=$error;
	}
	private function login_160by2($username,$password)
	{
	//	$out2=$this->curl->get("http://m.160by2.com");
		$out=$this->curl->post("http://m.160by2.com/LoginCheck.asp?l=1&txt_msg=&mno=","txtUserName=$username&txtPasswd=$password&RememberMe=Yes&cmdSubmit=Login");
		$pattern="/MyMenu.asp\?Msg=(.+?)&/";

		preg_match($pattern,$out,$matches);
		$id=trim($matches[1]);
		$this->data['id']=$id;

	}

	private function send_160by2($number,$msg)
	{
		$msg=urlencode($msg);
		$id=$this->data['id'];
		$out1=$this->curl->post("http://m.160by2.com/SaveCompose.asp?l=1","txt_mobileno=$number&txt_msg=$msg&cmdSend=Send+SMS&TID=&T_MsgId=&Msg=$id");
		//echo $out1;
		$pattern = '/\<table.+?\>(.+)\<\/table/s';
		preg_match($pattern, $out1, $matches);

		$out=strip_tags(@$matches[1]);
		if(count($matches)<1)
		{
		$pattern="/\<div.+?background:.+?yellow.+?\>(.+?)\<\/div\>/s";

		preg_match($pattern,$out1,$matches);

		$out=strip_tags($matches[1]);
		}

//
	//	echo "out is $out";

		if(!preg_match("/successfully/i",$out))
		{

		$this->setError($out);

		return false;
		}
		else
		{
		return true;
		$this->setError("No errors");
		}

	}

	private function login_airtel($username,$password)
	{
		$this->data['username']=$username;
		$this->data['password']=$password;

	}

	private function send_airtel($number,$msg)
	{

	}
}

?>

Now save all three file in same directory and send SMS free :) Thanks.

  • jitendra

    i am jitu and above code is error full and not solved easily

    View Comment
  • Biswajit Sinha

    Kya bekar ki script he.. . waste of time

    View Comment
  • Karthik Gk

    This code not working

    View Comment
  • Karthik Gk

    Logging in …
    Notice: Undefined offset: 1 in C:xampphtdocskarthikskyzonsmsclass.sms.php on line 129
    Sending SMS …
    Notice: Undefined offset: 1 in C:xampphtdocskarthikskyzonsmsclass.sms.php on line 150
    Error encountered :

    View Comment
    • Aj Gaurkhede

      plz help mi…m getting same error…

      View Comment
  • Ramesh

    FYI : extension=php_curl.dll is enabled

    View Comment
  • Ramesh

    Warning: fclose() expects parameter 1 to be resource, string given in C:xampphtdocsSMSclass.curl.php on line 38

    Logging in …

    Notice: Undefined offset: 1 in C:xampphtdocsSMSclass.sms.php on line 73

    Sending SMS …

    Notice: Undefined offset: 1 in C:xampphtdocsSMSclass.sms.php on line 91

    Notice: Undefined offset: 1 in C:xampphtdocsSMSclass.sms.php on line 96

    Error encountered :

    Getting the above error. Any ideas?

    View Comment
  • arpan

    Logging in …

    ( ! ) Notice: Undefined offset: 1 in C:wampwwwsmsclass.sms.php on line 73
    Call Stack
    #TimeMemoryFunctionLocation
    10.0008369856{main}( )..send.php:0
    20.0028471272sms->login( )..send.php:20
    30.0028471576call_user_func
    ( )..class.sms.php:45
    40.0028471592sms->login_way2sms( )..class.sms.php:45

    Sending SMS …

    ( ! ) Notice: Undefined offset: 1 in C:wampwwwsmsclass.sms.php on line 91
    Call Stack
    #TimeMemoryFunctionLocation
    10.0008369856{main}( )..send.php:0
    20.9593471504sms->send( )..send.php:23
    30.9593471808call_user_func
    ( )..class.sms.php:53
    40.9593471824sms->send_way2sms( )..class.sms.php:53

    ( ! ) Notice: Undefined offset: 1 in C:wampwwwsmsclass.sms.php on line 96
    Call Stack
    #TimeMemoryFunctionLocation
    10.0008369856{main}( )..send.php:0
    20.9593471504sms->send( )..send.php:23
    30.9593471808call_user_func
    ( )..class.sms.php:53
    40.9593471824sms->send_way2sms( )..class.sms.php:53

    Error encountered :

    Theese are my errors

    View Comment
  • Ramesh

    This is for testing. I am using XAMPP on local system. So where do I place all the 3 files??

    View Comment
    • deepu sama

      my computer
      local disk (C)
      xampp
      htdoc
      create a folder and name it place this files in that and compile you can get it if xampp works good

      View Comment
  • Dilip

    hello Shyam Sundar here some problem’s with my form i create php file 1 send.php ,2 class.sms.php ,3 class.curl.php

    error is like below

    “2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 Warning: fclose(): supplied argument is not a valid stream resource in C:wampwwwsmsdemoclass.curl.php on line 38
    Logging in …

    Fatal error: Call to undefined function curl_init() in C:wampwwwsmsdemoclass.curl.php on line 59”

    View Comment
    • Dharmalingam

      open php.ini file
      fine the following line in php.ini file.
      ;extension=php_curl.dll

      remove the ; before that line and save and execute.

      View Comment
      • deepu sama

        it was already removed even though it is not comming

        when i compile the result is in this form

        http://localhost/SMS/send.php

        Logging in …
        Notice: Undefined offset: 1 in C:xampphtdocsSMSclass.sms.php on line 126
        Sending SMS …
        Notice: Undefined offset: 1 in C:xampphtdocsSMSclass.sms.php on line 147
        Error encountered :

        it is in the below

        View Comment
  • elango

    Not working…

    View Comment
  • Bhavesh

    Warning: fclose() expects parameter 1 to be resource, string given in C:xampphtdocssmsclass.curl.php on line 38
    Logging in …
    Notice: Undefined offset: 1 in C:xampphtdocssmsclass.sms.php on line 126
    Sending SMS …
    Notice: Undefined offset: 1 in C:xampphtdocssmsclass.sms.php on line 147
    Error encountered :

    View Comment
  • harry matharoo

    Logging in …
    Notice: Undefined offset: 1 in E:xampphtdocsclass.sms.php on line 126
    Sending SMS …
    Notice: Undefined offset: 1 in E:xampphtdocsclass.sms.php on line 140

    Notice: Undefined offset: 1 in E:xampphtdocsclass.sms.php on line 147
    Error encountered :

    View Comment
  • http://ksmtourism.in Karamjeet

    these files does’nt working

    View Comment
  • http://ksmtourism.in Karamjeet

    Logging in …
    Notice: Undefined offset: 1 in C:xampphtdocsoopsnew_smsclass.sms.php on line 71

    Notice: Undefined offset: 1 in C:xampphtdocsoopsnew_smsclass.sms.php on line 79
    Sending SMS …
    Notice: Undefined offset: 1 in C:xampphtdocsoopsnew_smsclass.sms.php on line 98

    Notice: Undefined offset: 1 in C:xampphtdocsoopsnew_smsclass.sms.php on line 103
    Error encountered :

    View Comment
  • Gajanan

    Logging in …
    Notice: Undefined offset: 1 in C:wwwvhostslocalhostfree smsclass.sms.php on line 126
    Sending SMS …
    Notice: Undefined offset: 1 in C:wwwvhostslocalhostfree smsclass.sms.php on line 147
    Error encountered :

    View Comment
  • Vishnumoorthy

    Logging in …
    Notice: Undefined offset: 1 in C:wampwwwsms1class.sms.php on line 125
    Sending SMS …
    Notice: Undefined offset: 1 in C:wampwwwsms1class.sms.php on line 146
    Error encountered :

    View Comment
  • sanket

    Logging in …
    Fatal error: Call to undefined function curl_init() in C:xampphtdocssms2class.curl.php on line 59

    im getin this problem

    plz help

    View Comment
  • Ajay

    Logging in …
    Notice: Undefined offset: 1 in C:xampphtdocssms3class.sms.php on line 73
    Sending SMS …
    Notice: Undefined offset: 1 in C:xampphtdocssms3class.sms.php on line 91

    Notice: Undefined offset: 1 in C:xampphtdocssms3class.sms.php on line 96
    Error encountered :

    View Comment
  • http://softworkus.com Monojit Roy

    Logging in …
    Notice: Undefined offset: 1 in C:xampphtdocssms3class.sms.php on line 73
    Sending SMS …
    Notice: Undefined offset: 1 in C:xampphtdocssms3class.sms.php on line 91

    Notice: Undefined offset: 1 in C:xampphtdocssms3class.sms.php on line 96
    Error encountered :

    Please help me to get rid off these error as soon as possbile

    View Comment
  • http://www.pamtsoft.co.in Amol P Pawar

    Logging in …
    Notice: Undefined offset: 1 in D:AppServwwwamolclass.sms.php on line 73
    Sending SMS …
    Notice: Undefined offset: 1 in D:AppServwwwamolclass.sms.php on line 91

    Notice: Undefined offset: 1 in D:AppServwwwamolclass.sms.php on line 96
    Error encountered :

    View Comment
  • http://weoptimise.in krish

    Logging in … Sending SMS … Error encountered :

    Getting this error any solution?

    View Comment
  • saran

    Logging in …
    Notice: Undefined offset: 1 in C:xampphtdocsnewsmsclass.sms.php on line 126
    Sending SMS …
    Notice: Undefined offset: 1 in C:xampphtdocsnewsmsclass.sms.php on line 147
    Error encountered :
    please reply with answers
    126–> $id=trim($matches[1]);

    146–> $out=strip_tags($matches[1]);

    View Comment
  • Deepak

    Logging in …
    Notice: Undefined offset: 1 in C:\xampp\htdocs\sms3\class.sms.php on line 73
    Sending SMS …
    Notice: Undefined offset: 1 in C:\xampp\htdocs\sms3\class.sms.php on line 91

    Notice: Undefined offset: 1 in C:\xampp\htdocs\sms3\class.sms.php on line 96
    Error encountered :

    Please help me to get rid off these error as soon as possbile

    View Comment
  • http://www.comingsoonlive.com Magson Fernandes

    Thanks for the code to send free sms

    View Comment
  • Rahul Kumar

    Debendra first of all you upload this file to your server then right click on that file and goto the file permission check all the permissions which u required.
    hope this will fixed ur problem…

    View Comment
  • Debendra kumar sahoo

    how do i permission the cookie.txt file

    View Comment