aenugula karthik
aenugula karthik

Reputation: 359

Sending Sms From PHP Script

I am Sending Messages through my sms api using PHP Script. The Sms are sending Succesfully but i got a problem in my message. The Php Script only delivering of "ThankYou" Message remaining are not coming to the mobile.

 $message1="ThankYou '$email' you are succesfully booked your service.Your booking details Booking Date = '$date'Booking Location='$location'";

My PHP Script:

  <?php

 require 'PHPMailer/PHPMailerAutoload.php';
 require "init.php";
  $email=$_POST['email']; 
  $bikeno=$_POST['bikeno'];
  $location=$_POST['location'];
  $date=$_POST['date'];
  $mobileno=$_POST['mobileno'];
   $sql="select * from book_order where location ='".$location."' and date ='".$date."';";
  $result=mysqli_query($con,$sql);
  $response =array();
  if(mysqli_num_rows($result)>=20)
    {
   $code="reg_failed";
   $message="Sorry For that Selected Date or Service Centre Is also Booked try with another Date or Service centre";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode($response);    
     }
   else {
$sql="insert into book_order(email,bikeno,location,date,mobileno) values ('".$email."','".$bikeno."','".$location."','".$date."','".$mobileno."');";
$result=mysqli_query($con,$sql);
$code="reg_success";
$message="Thanks for choose use for serve you better.";
array_push($response,array("code"=>$code, "message"=>$message));
echo json_encode($response);
  if($sql)
{   
$message1="ThankYou '$email' you are succesfully booked your service.Your         booking details Booking Date = '$date'Booking Location='$location'";


   $URL = "http://ptms.bulksmshyderabad.in/API/sms.php?username=xxxx&password=xxxx&from=YAMAHA&to=$mobileno&msg=$message1&type=1&dnd_check=0";
    $ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result=curl_exec($ch);


   }    

  }


  mysqli_close($con);
    ?>

Upvotes: 0

Views: 5778

Answers (3)

K. Salabir
K. Salabir

Reputation: 34

http POST API is used to send sms. simplexml_load_file() function is used here to perform the process.


    $to="88".$this->mobile_no; //Country code concatenation
    $gsm =$to; // Formed as ISD number 
    $msg ="Thank you"; //SMS content that will be sent
    $user = 'úsername'; // API username
    $password = 'password'; //API password
    $sender =sender; // i.e.: 880155911456 or Sender id might be alfanumeric for masking
    $url = "http://api.olineit.com/api/v3/sendsms/plain?user=$user&password=$password&sender=$sender&SMSText=$msg&GSM=$gsm&"; //http REST API url
    $smsResult = simplexml_load_file($url); // Function called and parameter passed.

Upvotes: 0

vishnu sharma
vishnu sharma

Reputation: 72

I assume that you already have sms service from msg91 using above setup or you can use any sms service providers like Twilio, Nexmo etc. Now i am creating a common function which you can call anywhere in your PHP code to send any type of text sms.

//send otp message
public static function sendMessage($mobile, $message)
{
    $message = urlencode($message);
    $url = sprintf("http://api.msg91.com/api/v2/sendsms?authkey=%s&mobiles=%s&message=%s&sender=%s&route=%s",
        env('MSG91_AUTH_KEY'), $mobile, $message, env('MSG91_SENDER_ID'), env('MSG91_ROUTE'));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

For more details : https://www.lelocode.com/posts/sending-sms-like-otp,-welcome-message,mobile-verification-using-php---lelocode

Upvotes: 0

Chainat
Chainat

Reputation: 197

You have invalid characters (i.e. ') in the url. The SMS server is unable to parse your SMS message properly. Use the code below to make a query string safe/valid for a GET request.

$message1 = "..."; // Your original message
$safeMessage = urlencode($message1);

Of course, you need to use $safeMessage when assigning value to $URL.

Additional Notes:

  • Pls consider removing your sensitive credential from the question, i.e. real username and password in the url.
  • Your curl request is forming a POST request, however the GET request seems is sufficient because the API can take all info from the url.

Upvotes: 1

Related Questions