Step-by-step instructions for creating SMS web application using SMS API from Infobip

    Welcome to the Infobip instructions for creating your own SMS web application for beginners. We will help you step by step through the difficulties of installing the SMS API from Infobip and SMS services. The manual includes 3 examples of the most important options for sending SMS messages and checking their status:

    • Full-featured text messaging
    • Logs of sent messages
    • Delivery reports in Notify URL

    We will start with examples and presentations so that you can choose what you are most interested in.

    To follow this instruction, to write and test everything yourself, you need to prepare (and we do not mean to pour a cup of coffee and turn off the light). To send messages, receive logs and message delivery reports, you need to install the URL php extension for your web server.

    To work with this instruction, you can use the solution from the AMP stack (wamp, xampp, etc.). This software stack for various operating systems consists of the Apache web server, MySQL database and support for the PHP programming language. In any case, you must install the phpcurl extension .

    Note:to send SMS messages safely, these startup examples should be hosted on HTTPS (using TLS). To simplify the instructions, we used HTTP.

    Full-featured text messaging


    The text message page ( advancedSms.php ) contains a form for sending messages . The confirmation button will send a request to the form action attribute . In this example, she will send it to herself.

    image

    Query building


    Before you start changing values, you must check that you have set them correctly. In this example, we checked only the “toInput” field . You do not need to check them all, because the HTTP POST method will set everything automatically (if any of the input fields is empty, its value will be an empty string). If this is your first time loading a page, keep in mind that none of these fields will be present. We are just two steps away from creating the entire page. After checking the fields, you need to determine the URL for sending the request , as well as the body of the request to be sent. The body of the POST request will be represented as a JSON string. The formation of the request body will look like this:



    In the example, a message is sent to only one recipient. If you want to send this message to several addresses, all you need to do is create another destination and add it to the recipient list.

    Since "to" is the only required field, you need to check if it is filled before sending the request. If it is skipped, the user will receive a message about the empty field.

    image

    To send a request, we select cURL .

    image

    In the above code, we used many options to create a request that are initialized by curl_init () :

    • CURLOPT_URL - setting the URL using the endpoint method in it
    • CURLOPT_HTTPHEADER - setting the content type and request header
    • CURLOPT_HTTPAUTH, CURLOPT_USERPWD - authorization type, username and password
    • CURLOPT_POST - used HTTP method - POST
    • CURLOPT_POSTFIELDS - pre-created XML or JSON structured string

    • Here you can find other cURL sending options.

    When you set all the parameters, you must run the curl_exec ($ ch) request . This method will give you an answer that you can get in JSON and which you can use in future analysis. After completing the request, you will receive information about the HTTP response code, which we will use later in this instruction - curl_getinfo ($ ch, CURLINFO_HTTP_CODE) .

    Response analysis


    If everything went right, and you received an HTTP response code (200 OK 201 CREATED, etc.), you can extract the necessary information from the response body and present it to the user. In our example, we decided to introduce: Message ID, To, SMS Count, Status Group, Status Group Name, Status ID, Status Name and Status Description, but you can choose what you need.

    In the foreach loop below, a single line response will be shown for each message:

    image

    image

    Logs of sent messages


    When this option is selected, the logs.php page opens with an input form for receiving logs of sent messages . The confirmation button will publish these fields to itself.

    Query building


    logs.php is a page where logs of sent messages will be presented. You must specify the URL to send the request with sent message logs endpoint, and send a request to it with GET HTTP method this time (CURLOPT_HTTPGET option set to TRUE):

    image

    After the request is completed, culr_exec ($ curl) , and receive the HTTP response code, you You can start the analysis of the body depending on the required field of the response code, similar to what we did in the chapter on full-featured text messages .

    Response analysis


    If everything went right, and you received an HTTP response code (200 OK 201 CREATED, etc.), you can extract the necessary information from the response body and present it to the user. In our example, we chose: Message ID, To, From, Text, Status Group Name, Status Description and Sent At , but you can choose what you need, just like before:

    image

    The next foreach loop will be compiled for each field of the array of logs of sent messages and is inscribed in one line with the corresponding columns in it. In this case, a foreach loop for each field is necessary because we requested it for all message logs.

    In exceptional cases, you can analyze the request body in the same way as we did in the message sending method:

    image

    Delivery reports in Notify URL


    This function is slightly different from the previous two - the dlrPush.php page is not used to request data, it is waiting for it. When data is “pushed” to this page, it can be analyzed and displayed to the user accordingly.

    Note: Delivery reports are “pushed” from the full-featured text message page by entering the URL of this page in the Notify URL field. In addition, the Notify ContentType field is filled in, with which the page determines what type of body should be obtained.

    Receive Delivery Reports


    image

    The code above shows that file_get_contents ('php: // input') is used to receive raw POST data as a string. The following lines show you how to check the data provided in XML or JSON, and how to extract delivery reports. For XML, we analyze whether the line of the response body starts with, and if not, try to decrypt it without errors - the function isJson () If all conditions are FALSE, the $ result variable remains unset. This means that we must tell the user that the delivery report was not published on the callback server.

    Results Analysis


    The analysis of delivery reports is very similar to the analysis of the response of full-featured text messages and the logs of sent messages , except that we do not check the HTTP response code (because there is no answer at all). All we have to do is select what information from the delivery report we want to show and write it on the page.

    Also popular now: