Code example in programming language PHP for SMS API integration

Below you will find the examples of programming code in the language PHP to send SMS messages through the API of the LabsMobile platform.

You also have code examples in JavaScript, Python and other programming languages.

We recommend you to consult and take into account the following resources and help in your integration:

Send SMS - REST API - JSON

This is the sample code in PHP to send SMS messages with the LabsMobile SMS API in the JSON version. As you can see, you must create a JSON structure with all the sending parameters and make an HTTP/POST call with the JSON data in the body of the call.

It is essential to make the call with the authentication of the username (registration email) and the password (or token API) of the account.

            
<?php
$auth_basic = base64_encode("myusername:mypassword");

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.labsmobile.com/json/send",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => '{"message":"Text of the SMS message", "tpoa":"Sender","recipient":[{"msisdn":"12015550123"},{"msisdn":"447400123456"},{"msisdn":"5212221234567"}]}',
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic ".$auth_basic,
    "Cache-Control: no-cache",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

You can check all available JSON parameters, configuration options, manuals and specifications in the following url: https://apidocs.labsmobile.com/?php#send-sms

Credit inquiry - REST API - JSON

Example of code in PHP for consulting the balance of an account. The result is always obtained in internal credits of the LabsMobile platform.

            
<?php

$auth_basic = base64_encode("myusername:mypassword");

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.labsmobile.com/json/balance",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic ".$auth_basic,
    "Cache-Control: no-cache",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Send SMS - HTTP/GET

Example of sending for the GET version of the LabsMobile SMS API. This is a basic and simple method of sending SMS messages from an application or software created in PHP passing all the parameters in the same url.

It is important to encode all the values ​​as url (with the function urlencode() for example).

You can see and download the GET API manual of LabsMobile in the following URL: https://www.labsmobile.com/en/api-sms/api-versions/http-get

                
<?php
    $message = '';
    if (isset($_POST['message'])) {

        if (empty($_POST['msisdn']) || empty($_POST['message'])) {
            $message = 'All fields need to be filled in';
        } else {
            $url = 'http://api.labsmobile.com/get/send.php?';
            $url .= 'username=acct01@demo.com&';
            $url .= 'password=acct01pwd&';
            $url .= 'msisdn=' . $_POST['msisdn'] . '&';
            $url .= 'message=' . $_POST['message'] . '&';

            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            $result = curl_exec($ch);
            curl_close($ch);

            $message = htmlentities('Message has been sent.<br />Details:' . "<br />" . $result);
        }
    }
?>
<html>
    <head>
        <title>SMS Example</title>
    </head>
    <body>
        <p><?php echo $message ?></p>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
            <p>MSISDN: <input type="text" value="" name="msisdn" /></p>
            <p>Message: <input type="text" value="" maxlength="160" name="message" /></p>
            <p><input type="submit" value="Send SMS" name="send" /></p>
        </form>
    </body>
</html>

Credit inquiry - HTTP/GET

                
<?php
    if (isset($_POST['send'])) {
        $url = 'http://api.labsmobile.com/get/balance.php?username=acct01&password=acct01pwd';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $result = curl_exec($ch); curl_close($ch);
        $message = htmlentities('Message has been sent.<br />Details:' . "<br />" . $result);
     }
?>
<html>
    <head>
        <title>SMS Example credit query</title>
    </head>
    <body>
        <p><?php echo $message ?></p>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
            <p><input type="submit" value="Check credit" name="send" /></p>
        </form>
</body>

Send SMS - HTTP/POST XML

                
<?php
    $message = '';
    if (isset($_POST['send'])) {
        if (empty($_POST['msisdn']) || empty($_POST['message'])) {
            $message = 'All fields need to be filled in';
        } else {
            $url = 'http://api.labsmobile.com/clients/';
            $username = 'acct01';
            $password = 'acct01pwd';
            $sms = '<?xml version="1.0" encoding="UTF-8"?>
                <sms>
                <recipient>
                    <msisdn>:MSISDN</msisdn>
                </recipient>
                <message><![CDATA[:MESSAGE]]></message>
                <acklevel>handset</acklevel>
                <ackurl>http://clientserver.com/motest.php</ackurl>
                </sms>';
            $sms = utf8_encode(str_replace(array(':MSISDN', ':MESSAGE'), array($_POST['msisdn'], $_POST['message']), $sms));

            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, 'XmlData='.$sms);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            $result = curl_exec($ch); curl_close($ch);
            $message = htmlentities('Message has been sent.<br />Details:' . "<br />" . $result);
        }
    }
?>
<html>
    <head>
        <title>SMS Example</title>
    </head>
    <body>
        <p><?php echo $message ?></p>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
            <p>MSISDN: <input type="text" value="" name="msisdn" /></p>
            <p>Message: <input type="text" value="" maxlength="160" name="message" /></p>
            <p><input type="submit" value="Send SMS" name="send" /></p>
        </form>
    </body>

Credit inquiry - HTTP/POST

                
<?php
    if (isset($_POST['send'])) {
        $url = 'http://api.labsmobile.com/balance.php';
        $username = 'acct01';
        $password = 'acct01pwd';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $result = curl_exec($ch); curl_close($ch);

        $message = htmlentities('Message has been sent.<br />Details:' . "<br />" . $result);
    }
?>
<html>
    <head>
        <title>SMS Example credit query</title>
    </head>
    <body>
        <p><?php echo $message ?></p>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
            <p><input type="submit" value="Check credit" name="send" /></p>
        </form>
</body>

Reception confirmation ACK

                
<?php
    $acklevel = $_GET['acklevel'];
    $msisdn = $_GET['msisdn'];
    $status = $_GET['status'];
    $subid = isset($_GET['subid']) ? $_GET['subid'] : '';
    $timestamp = $_GET['timestamp'];

    $string = "At $timestamp we recieved a";
    if ($status == 'ok') {
        $string .= ' positive ';
    } else {
        $string .= ' negative ';
    }
    $string .= "$acklevel ack with $msisdn as the receiver\n";

    file_put_contents('ack.txt', $string, FILE_APPEND);
?>

Send SMS - WebService

                
<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    require_once('../lib/nusoap.php');
    $proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
    $proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
    $proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
    $proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';

    $client = new nusoap_client('http://api.labsmobile.com/ws/services/LabsMobileWsdl.php?WSDL', false, $proxypassword);
    $err = $client->getError();
    if ($err) {
        echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    }
    $client->soap_defencoding = 'UTF-8';

    $funciones = array();
    $params = array();
    $result = $client->call('SendSMS', array('client' => "priv001", 'username' => "acct01@demo.com", 'password' => "acct01pwd", 'xmldata' => "
            <?xml version="1.0" encoding="UTF-8"?><sms>
            <recipient>
                <msisdn>34609542312</msisdn> </recipient>
                <message><![CDATA[Test message number 1]]></message>
                <tpoa><![CDATA[Sender name]]></tpoa> </sms>"), '', '', false, true);

    if ($client->fault) {
        echo '<h2>Fault</h2><pre>';
        print_r($result);
        echo '</pre>';
    } else {
        $err = $client->getError();
        if ($err) {
            echo '<h2>Error</h2><pre>' . $err . '</pre>';
        } else {
            echo '<h2>Result</h2><pre>';
            print_r($result);
            echo '</pre>';
        }
    }
    echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
    echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
    echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
  • contact form support

    Sign up!

    Create a user account and send your SMS messages in seconds. You will have all the functionalities and benefits in the same platform.

    Send from API and manage your account with our online application WebSMS.

    Create new account
  • Hey Petit, Services for rural accommodation recommends LabsMobile

    SMS confirmation and reservation reminders for rural accommodation using an online portal.

    Watch more reviews
  • dashboard aplicación online

    Maximum reliability at the best price

    At LabsMobile we only offer direct routes of maximum reliability and quality. Enjoy our platform and all our services for the price of an SMS.

    Pay ONLY for sent messages.

    Check our rates
  • dashboard aplicación online

    Need more info? Contact us!

    Our technical department has professionals with years of experience and we have made multiple integrations.

    We guide and help you through the process.

    Request technical support
  • Get help or support

    This tutorial explains how to resolve any doubt or incident from a user of the LabsMobile platform.

    Ir al tutorial