The TeleMessage PHP library lets you write PHP code to make REST requests to the TeleMessage REST API. 

Download Code

Download code from TeleMessage Github.

 

Using the Library

There are 2 ways to use TeleMessage PHP Library:

  1. Using phar archive (since PHP 5.2.0). Find it in github root folder: telemessage.phar
  2. Using Library source code by including files in your own project.

 

Sending Messages

The first step is to include TeleMessage in your project: 

using phar: require_once "telemessage.phar";

using sources: require_once ("com/TeleMessage.class.php");

 

Next step is to initialize TeleMessage object: $tm = TeleMessage::get();

Now we need to create object with TeleMessage account credentials:

$auth = new AuthenticationDetails();
$auth->setUsername("john_donne");
$auth->setPassword("12345678");

Let’s fill in the message data.

First, we set text and subject:

$m = new Message();
$m->setSubject("Hello");
$m->setTextmessage("My message");

Than we create recipient and add it to Message:

$recp = new Recipient();
$recp->setType("SMS");
$recp->setValue("+1xxxxxxxx");
$m->addRecipient($recp);

and the next step, we generate REST to be send to TeleMessage: $data = $tm->generateSend($auth, $m);

Message is ready and now we need to send it to TeleMessage. In our example we use CURL for sending message to TeleMessage gateway:

First, let’s create headers:

    $myHeader = array(
        "MIME-Version: 1.0",
        "Content-type: text/json; charset=utf-8"
    );

 Finally, we initialize CURL and POST $data to TeleMessage::SEND_URL:

    //creating and initiating curl
    $ch = curl_init();
    //setting curl/http headers
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_URL, TeleMessage::SEND_URL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
    $postResult = curl_exec($ch);
    curl_close($ch);

The last small step is to parse response and find TeleMessage messageId and messageKey:

    if ($postResult != "") {
        $res = $tm->getResponse($postResult);
        echo "Result code: " . $res->getResultCode();
        if ($res->getResultCode() == TeleMessage::SUCCESS_SEND) {
            echo ", Message key: " . $res->getMessageKey() . ", message id: " . $res->getMessageID();
        } else {
            echo ", Result description: " . $res->getResultDescription();
        }
    }

  That’s it – message is sent!

 

Sending Messages with Attachments

Sending message with attachments is very similar to sending a simple message. The only thing you need to do is add your file by using the addFileMessage method:

    $fm = new FileMessage();
    $fm->setFilename("file.png");
    $fm->setMimetype("image/png");
    $imgPath = pathinfo(__FILE__, PATHINFO_DIRNAME) . "/file.png";
    $fm->setValue(base64_encode(file_get_contents($imgPath)));

    $m->addFilemessage($fm);

Note:

  1. Some file types won’t be supported by your destination, e.g. if you are sending tiff file to SMS, the attachment won’t be added, however the same file will be delivered to a fax recipient.
  2. You must encode your attachment file into Base64. Find more here.

Query Status

Great! Message is sent, but status you’ve just received, saying that message “Not delivered yet”. Sending message could take few seconds, so we added queryStatus in our REST API and you can use it with our PHP API Library. See how:

If you are using different from “Sending Message” script, first you need to include TeleMessage in your project: 

using phar: require_once "telemessage.phar";

using sources: require_once ("com/TeleMessage.class.php");

 

Next step is to initialize TeleMessage object: $tm = TeleMessage::get();

Now we need to create object with TeleMessage account credentials:

$auth = new AuthenticationDetails();
$auth->setUsername("john_donne");
$auth->setPassword("12345678");

and the next step, we generate REST to be send to TeleMessage: $data = $tm->generateQueryStatus($auth, $messsageId, $messageKey);

Note: Use $messageID and $messageKey from response to Sending Message

Again we are using CURL for POSTing request to TeleMessage gateway:

First, let’s create headers:

    $myHeader = array(
        "MIME-Version: 1.0",
        "Content-type: text/json; charset=utf-8"
    );

Finally, we initialize CURL and POST $data to TeleMessage::STATUS_URL:

    //creating and initiating curl
    $ch = curl_init();
    //setting curl/http headers
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_URL, TeleMessage::STATUS_URL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
    $postResult = curl_exec($ch);
    curl_close($ch);

Now, we need to parse response and print it out:

if ($postResult != "") {
        $res = $tm->getResponse($postResult);
        echo "Result code: " . $res->getResultCode();
        if ($res->getResultCode() == TeleMessage::SUCCESS_REQUEST) {
            $recipients = $res->getRecipientStatus();
            if (count($recipients) > 0) {
                foreach ($recipients as $status) {
                    echo ", Message sent to  " . $status->getRecipient()->getValue() .
                        " with status " . $status->getStatus() . " that means " . $status->getDescription() .
                        " at " . date("y/m/d i:h:s", $status->getStatusDate());
                }
            }
        } else {
            echo "Result description: " . $res->getResultDescription();
        }
    }

 That’s it. We hope it was helpful and now you can use TeleMessage services more easily. If you still have questions, please try our FAQ

Complete Code Cxamples

  •     // initializing data and autoloading required files
        $tm = TeleMessage::get();
    
        $auth = new AuthenticationDetails();
        $auth->setUsername("john_donne");
        $auth->setPassword("12345678");
    
        $recp = new Recipient();
        $recp->setType("SMS");
        $recp->setValue("+1-xxx-xxxxxx");
    
        $m = new Message();
        $m->addRecipient($recp);
        $m->setSubject("Hello");
        $m->setTextmessage("My message");
    
        $data = $tm->generateSend($auth, $m);
    
        //creating header for http post request
        $myHeader = array(
            "MIME-Version: 1.0",
            "Content-type: text/json; charset=utf-8"
        );
        //creating and initiating curl
        $ch = curl_init();
        //setting curl/http headers
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_URL, TeleMessage::SEND_URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
        $postResult = curl_exec($ch);
        curl_close($ch);
    
        if ($postResult != "") {
            $res = $tm->getResponse($postResult);
            echo "Result code: " . $res->getResultCode();
            if ($res->getResultCode() == TeleMessage::SUCCESS_SEND) {
                echo ", Message key: " . $res->getMessageKey() . ", message id: " . $res->getMessageID();
            } else {
                echo ", Result description: " . $res->getResultDescription();
            }
        }

     

  •     // initializing data and autoloading required files
        $tm = TeleMessage::get();
    
        $auth = new AuthenticationDetails();
        $auth->setUsername("john_donne");
        $auth->setPassword("12345678");
    
        $recp = new Recipient();
        $recp->setType("EMAIL");
        $recp->setValue("someemail@somedomain.com");
    
        $m = new Message();
        $m->addRecipient($recp);
        $fm = new FileMessage();
        $fm->setFilename("file.png");
        $fm->setMimetype("image/png");
        $imgPath = pathinfo(__FILE__, PATHINFO_DIRNAME) . "/file.png";
        $fm->setValue(base64_encode(file_get_contents($imgPath)));
    
        $m->addFilemessage($fm);
    
        $data = $tm->generateSend($auth, $m);
        //creating header for http post request
        $myHeader = array(
            "MIME-Version: 1.0",
            "Content-type: text/json; charset=utf-8"
        );
        //creating and initiating curl
        $ch = curl_init();
        //setting curl/http headers
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_URL, TeleMessage::SEND_URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
        $postResult = curl_exec($ch);
        curl_close($ch);
    
        if ($postResult != "") {
            $res = $tm->getResponse($postResult);
            echo "Result code: " . $res->getResultCode();
            if ($res->getResultCode() == TeleMessage::SUCCESS_SEND) {
                echo ", Message key: " . $res->getMessageKey() . ", message id: " . $res->getMessageID();
            } else {
                echo ", Result description: " . $res->getResultDescription();
            }
        }

    Note:

    1. Some file types won’t be supported by your destination, e.g. if you are sending tiff file to SMS, the attachment won’t be added, however the same file will be delivered to a fax recipient.
    2. You must encode your attachment file into Base64. Find more here.
  •     // initializing data and autoloading required files
        $tm = TeleMessage::get();
    
        $auth = new AuthenticationDetails();
        $auth->setUsername("john_donne");
        $auth->setPassword("12345678");
    
        $messsageId = 352866230;
        $messageKey = "392449495831987710820990896274";
    
        $data = $tm->generateQueryStatus($auth, $messsageId, $messageKey);
    
        //var_dump($data);
    
        //creating header for http post request
        $myHeader = array(
            "MIME-Version: 1.0",
            "Content-type: text/json; charset=utf-8"
        );
    
        //creating and initiating curl
        $ch = curl_init();
        //setting curl/http headers
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_URL, TeleMessage::STATUS_URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
        $postResult = curl_exec($ch);
        curl_close($ch);
    
        if ($postResult != "") {
            $res = $tm->getResponse($postResult);
            echo "Result code: " . $res->getResultCode();
            if ($res->getResultCode() == TeleMessage::SUCCESS_REQUEST) {
                $recipients = $res->getRecipientStatus();
                if (count($recipients) > 0) {
                    foreach ($recipients as $status) {
                        echo ", Message sent to  " . $status->getRecipient()->getValue() .
                            " with status " . $status->getStatus() . " that means " . $status->getDescription() .
                            " at " . date("y/m/d i:h:s", $status->getStatusDate());
                    }
                }
            } else {
                echo "Result description: " . $res->getResultDescription();
            }
        }

 

Explore more API services:

Java LibraryREST APISOAP APIXML APIHTTP API