Search code examples
curlsmssms-gateway

Yii2 sms gateway- Sms is not going


I have sms gateway which is used to send sms on Yii2 project. The sms is not going when i call that particular action where sms code is stored. if i print that sms url and copy paste in browser and hit it.. its going. I don't know whats wrong in Yii2.

Below is the action code and curl code is in model

  public function actionMessage() {  
        /*sms code start*/ 
                $model = new BillPersonal();

                $mobile = "9703843454";
                $authKey = "XXXXXXXXXXXXXX";
                $senderId = "XXXXXX";
                $textMessage = "Test Messsage"; 

                $url = sprintf("http://www.smsgatewayhub.com/api/mt/SendSMS?APIKey=".$authKey."&senderid=".$senderId."&channel=2&DCS=0&flashsms=0&number=91".$mobile."&text=".$textMessage."&route=11"); 

                $curl_handle = curl_init();
                      curl_setopt($curl_handle, CURLOPT_URL, $url);
                      curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
                      curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
                      curl_setopt($curl_handle, CURLOPT_USERAGENT, 'CHMS');
                      $query = curl_exec($curl_handle);
       }

Solution

  • I finally got the solution.. The problem was with CURL settings. Hope this help someone.

    public function actionSms(){
    
                   $apikey = "XXXXXXXXXXXXXXXXXXX";
                   $apisender = "XXXXXXXXX";
                   $msg ="YOUR MESSAGE";
                   $num = "91XXXXXXXXXXXX";     
    
                   $ms = rawurlencode($msg);   
    
                $url = 'https://www.smsgatewayhub.com/api/mt/SendSMS?APIKey='.$apikey.'&senderid='.$apisender.'&channel=2&DCS=0&flashsms=0&number='.$num.'&text='.$ms.'&route=1';
    
                 //echo $url;
                 $ch=curl_init($url);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                 curl_setopt($ch,CURLOPT_POST,1);
                 curl_setopt($ch,CURLOPT_POSTFIELDS,"");
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER,2);
                 $data = curl_exec($ch);
                 echo '<br/><br/>';
                 print($data); /* result of API call*/
          }