Send SMS using Infobip API | PHP | Curl call


Recently I used Infobip SMS gateway for my client. It is Wocommerce website. so we are sending SMS communication for various Wocommerce events.
Hope this sample curl code will help you.

Requirement for SMS send using API.
1) Infobip account and Authentication Key
2) Approved SMS templates
3) Approved DLT
4) Approved Sender Name

PHP Code as follows.

//call a function to send SMS
sendSMSNotification($to, $name, $smsType, $var1, $var2);

function sendSMSNotification($to, $name, $smsType, $var1, $var2){

    $messageId = <UNIQUE_MESSAGE_ID>;
    $notifyUrl = "<NOTIFY_URL>";

    $dltArray = ['DLT_ID_1','DLT_ID_2','DLT_ID_3','DLT_ID_4','DLT_ID_5','DLT_ID_6','DLT_ID_7','DLT_ID_8','DLT_ID_9'];

//List of events for sending SMS communications.
    $smsTypeArray = ['shipped','delivered','feedback','abandonmentcart','cancelled','outfordelivery','orderconfirmed','passwordresetotp','registrationotp'];
    
//List of approved SMS templates
    $msgTemplatesArray = [
    "Hi {$name}. Your order has shipped. Keep track of your delivery here: {$var1}{$var2}",
    "Hi {$name}, Your order from us has been delivered. You can view your order here: {$var1}{$var2}",
    "Hi {$name}, It's been a week since your order was delivered. Would you take one minute to tell us about your experience? You can leave a review here: {$var1}{$var2}",
    "Hi {$name}, You left some items in your cart! We wanted to make sure you had the chance to get what you needed. Shop now: {$var1}{$var2}",
    "Hi {$name}. Heads up - Your order from {$var1} has been cancelled now. Please check status here {$var2}",
    "Hi {$name}. Heads up  - Your order from {$var1} is out for delivery and will arrive soon",
    "Thanks for shopping with us! Your order is confirmed and will be shipped shortly. Check your status here: <WEBSITE_URL>",
    "To reset your password, validate with this OTP, {$var1}. <CLIENT_NAME>",
    "Thank you for registering on <CLIENT_NAME> website. Your OTP is {$var1}. Validate within 15 minutes. Do not share OTP with any one. <WEBSITE_URL>"
    ];

    $key = array_search($smsType,$smsTypeArray);

    $destination = array("messageId" => $messageId, "to" => $to);

    $message = array(
        "from" => '<SENDER_ID>',
        "indiaDltContentTemplateId" => $dltArray[$key],
        "indiaDltPrincipalEntityId" => '<ENTITY_ID_Number>',
        "destinations" => array($destination),
        "text" => $msgTemplatesArray[$key],
        "notifyUrl" => $notifyUrl,
        "notifyContentType" => 'application/json',
        "callbackData" => '');

    $postData = array("messages" => array($message));
    $postDataJson = json_encode($postData);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, '<INFOBIP_BASE_URL>/sms/2/text/advanced');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
    $headers = array();
    $headers[] = 'Authorization: App <INFOBIP_KEY>';
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Accept: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
        return 0;
    }
    curl_close($ch);

    //return $result;
    return 1;

}


0 comments :

Send SMS using msg91 API | PHP | Curl call




Msg91 is a my preferred SMS gateway. I used in most of the Apps and websites.

Requirement for SMS send using API.
1) MSG91 account and Authentication Key
2) Approved SMS templates
3) Approved DLT
4) Approved Sender Name

PHP Code as follows.

//below is the post-fields 
//json_string = "{ \"sender\": \"<SENDER_NAME>\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"Hi, Your OTP is $num for verification on test.\", \"to\": [ \"$phone\" ] } ] }",

$postDataJson = json_encode($postData);

$authentication_key = "<MSG91_Authntication_Key>";    

//phone number with country code prefix
//here 91 in country code for India
$phone = '910000000000';

$postData = array(
    "sender" => "<SENDER_NAME>", 
    "route" => "4", 
    "country" => "91", 
    "sms" => array(
        "message" => "Hi, Your OTP is {$otp} for verification on {$var2}.",
        "to" => [$phone]
    )
);

$curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.msg91.com/api/v2/sendsms",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $postDataJson,
      CURLOPT_SSL_VERIFYHOST => 0,
      CURLOPT_SSL_VERIFYPEER => 0,
      CURLOPT_HTTPHEADER => array(
        "authkey: $authentication_key",
        "content-type: application/json"
    ),
  ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

0 comments :