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 :