Example code for SMS integration in language PHP

Send SMS - REST API - JSON

            
<?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;
}

Credit inquiry - REST API - JSON

            
<?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

                
<?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 = '<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' => "
        <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
  • DentalCare, Software for dental surgeries recommends LabsMobile

    SMS communication for dental surgeries. Reminders, loyalty programs, commercial and bulk marketing campaigns.

    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
  • Create a personalized SMS sending

    This tutorial explains how to send SMS messages to a mobile phone database by customizing the message text with variables.

    Ir al tutorial
loader