title: SMS sending through free gateway
body: <p>Please guide me with sample code for sending sms through website. How to include sms gateway. Please tell me, can i use way2sms.com as gateway?</p>
  
  <p>is it the right way to use? please suggest me only this much of code works?</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL);
  ob_implicit_flush(true);
  
  
  include_once "class.curl.php";
  include_once "class.sms.php";
  
  $smsapp=new sms();
  $smsapp-&gt;setGateway('way2sms'); 
  
  
  echo "Logging in  ... ";
  $smsapp-&gt;login('10 digit number','way2sms password');
  
  echo "Sending SMS ... ";
  $result=$smsapp-&gt;send('receipient number','text message');
  
  if($result=='true')
  {
      echo "Message sent";
  }
  else
  {   
      echo "Error encountered : ".$smsapp-&gt;getLastError();
  }
  
  ?&gt;
  </code></pre>
  
  <p>here is my class.curl.php</p>
  
  <pre><code>&lt;?php
  class sms
  {
      var $username;
      var $password;
      var $curl;
      var $server;
      var $loginDone;
      var $debugMode;
      var $data;
      var $error;
      public function __construct()
      {
          $this-&gt;curl=new cURL();
      //  $this-&gt;curl-&gt;setProxy("");
          $this-&gt;loginDone=false;
          $this-&gt;debugMode=false;
          $this-&gt;data=array();
      }
      public function setGateway($serverName)
      {
          switch($serverName)
          {
              case '160by2':
              $this-&gt;server='160by2';
              break;
  
              case 'way2sms':
              $this-&gt;server='way2sms';
              break;
  
              case 'airtel':
              $this-&gt;server='airtel';
              break;
  
              default :
              $this-&gt;server='way2sms';
  
          }
      }
      public function login($username,$password)
      {
          $server=$this-&gt;server;
  
          call_user_func(array($this,"login_$server"),$username,$password);
          $this-&gt;loginDone=true;
  
      }
      public function send($number,$msg)
      {
          $server=$this-&gt;server;
          if($this-&gt;loginDone)
          return call_user_func(array($this,"send_$server"),$number,$msg);
          else
          {
              echo "&lt;h2&gt;Please login first before sending SMS&lt;/h2&gt;";
          }
  
      }
      private function login_way2sms($username,$password)
      {
          $out=($this-&gt;curl-&gt;post("http://www.way2sms.com","1=1"));
          $pattern="/Location:(.+?)\n/";
          preg_match($pattern,$out,$matches);
          $domain=trim($matches[1]);
  
          $this-&gt;data['domain']=$domain;
  
          $out= $this-&gt;curl-&gt;post("{$domain}auth.cl","username=$username&amp;password=$password&amp;Submit=Sign+in");
  
          $pattern="/Location:(.+?)\n/";
          preg_match($pattern,$out,$matches);
          $referer=trim($matches[1]);
          $this-&gt;data['referer']=$referer;
  
  
      }
  
  
      private function send_way2sms($number,$msg)
      {
          $domain=$this-&gt;data['domain'];
          $html=$this-&gt;curl-&gt;post("{$domain}jsp/InstantSMS.jsp?val=0","1=1",$this-&gt;data['referer']);
          if($this-&gt;debugMode)
          {
          echo "&lt;h2&gt;After logging in, the HTML returned by server is&lt;/h2&gt;";
          echo $html;
          }
  
          $pattern = '/name="Action".+?value="(.*)"/';
          preg_match($pattern, $html, $matches);
  
          $custfrom=$matches[1];
          $msg=urlencode($msg);
          $html=$this-&gt;curl-&gt;post("{$domain}FirstServletsms?custid=","custid=undefined&amp;HiddenAction=instantsms&amp;Action={$custfrom}&amp;login=&amp;pass=&amp;MobNo=$number&amp;textArea=$msg");
          $pattern = '/class="style1"&gt;(.+?)&lt;\/span&gt;/';
          preg_match($pattern, $html, $matches);
          $out=($matches[1]);
  
          if(!preg_match("/successfully/",$out))
          {
          $this-&gt;setError($out);
          return false;
          }
          else
          {
          return true;
          $this-&gt;setError("No errors");
          }
  
      }
      public function getLastError()
      {
          return $this-&gt;error;
  
      }
      private function setError($error)
      {
          $this-&gt;error=$error;
      }
      private function login_160by2($username,$password)
      {
      //  $out2=$this-&gt;curl-&gt;get("http://m.160by2.com");
          $out=$this-&gt;curl-&gt;post("http://m.160by2.com/LoginCheck.asp?l=1&amp;txt_msg=&amp;mno=","txtUserName=$username&amp;txtPasswd=$password&amp;RememberMe=Yes&amp;cmdSubmit=Login");
          $pattern="/MyMenu.asp\?Msg=(.+?)&amp;/";
  
          preg_match($pattern,$out,$matches);
          $id=trim($matches[1]);
          $this-&gt;data['id']=$id;
  
      }
  
      private function send_160by2($number,$msg)
      {
          $msg=urlencode($msg);
          $id=$this-&gt;data['id'];
          $out1=$this-&gt;curl-&gt;post("http://m.160by2.com/SaveCompose.asp?l=1","txt_mobileno=$number&amp;txt_msg=$msg&amp;cmdSend=Send+SMS&amp;TID=&amp;T_MsgId=&amp;Msg=$id");
          //echo $out1;
          $pattern = '/\&lt;table.+?\&gt;(.+)\&lt;\/table/s';
          preg_match($pattern, $out1, $matches);
  
          $out=strip_tags(@$matches[1]);
          if(count($matches)&lt;1)
          {
          $pattern="/\&lt;div.+?background:.+?yellow.+?\&gt;(.+?)\&lt;\/div\&gt;/s";
  
          preg_match($pattern,$out1,$matches);
  
          $out=strip_tags($matches[1]);
          }
  
  //
      //  echo "out is $out";
  
          if(!preg_match("/successfully/i",$out))
          {
  
          $this-&gt;setError($out);
  
          return false;
          }
          else
          {
          return true;
          $this-&gt;setError("No errors");
          }
  
      }
  
      private function login_airtel($username,$password)
      {
          $this-&gt;data['username']=$username;
          $this-&gt;data['password']=$password;
  
      }
  
  
      private function send_airtel($number,$msg)
      {
  
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>in the place of username and password, which username and password i should use?</p>