News from the Developer Garden
Get up-to-date with all the news on our APIs and SDKs, new developer projects and current events.

How to: Develop Typo3 extensions with the Developer Garden Send SMS API

Nov 30th

TYPO3 is a flexible content management system for which there are numerous add-ons or “extensions.” Nearly all of the content users see in the back-end or front-end is provided using extensions.
This article is a step-by-step guide for developing TYPO3 extensions with the Developer Garden Send SMS API.

1. Creating a basic extension

To create a “blank” extension, you can use the Kickstarter extension. The important settings in Kickstarter are “extension key” and “category.” In our example, we used “sendsms” as the extension key and selected the category “frontend plug-in”. Selecting this category ensures that the extension can be added to pages in the form of a content element (page content). Next, we created a new front-end plug-in in Kickstarter. All of the files belonging to the extension are now found in the directory /typo3conf/ext/sendsms/.

2. Connecting the PHP SDK

TYPO3 coding guidelines recommend saving all of the libraries used by an extension in the lib folder. To send text messages, the SDK only requires two directories: /src/common and /src/sendsms. These are then copied to the folder /typo3conf/ext/sendsms/lib.

Next, write  in the Include section of the PHP script “/sendsms/pi1/class.tx_sendsms_pi1.php”:

require_once(dirname(__FILE__) . '/../lib/sdk/sendsms/client/SendSmsClient.php'); 
require_once(dirname(__FILE__) . '/../lib/sdk/sendsms/data/SendSmsStatusConstants.php');

Important note:

Caching must be disabled in order to send text messages from a front-end page. You can of course do this using the global TSFE object:

$GLOBALS['TSFE']->set_no_cache();

This is not very practical, though, since the entire page with the plug-in would not be saved in the cache as a result. It’s better to include the following text in the “main” function:

$this->pi_USER_INT_obj = 1;

and then in the definition of the class, to delete the following line:

$pi_checkCHash = TRUE; 

In the “ext_localconf.php” file, replace:

t3lib_extMgm::addPItoST43($_EXTKEY, 'pi1/class.tx_yourext_pi1.php', '_pi1', 'list_type', 1);

with the following:

t3lib_extMgm::addPItoST43($_EXTKEY, 'pi1/class.tx_yourext_pi1.php', '_pi1', 'list_type', 0);

This will disable caching only for the plug-in. Now the cache has to be deleted from the TYPO3 back-end a single time (click the lightning symbol at the top edge of the page).

3. Implementing the sending of text messages

The “main” function already contains an HTML form created automatically by Kickstarter for each new plug-in.

$content = '
    <strong>This is a few paragraphs:</strong><br />
    <p>This is line 1</p>
    <p>This is line 2</p>
    <h3>This is a form:</h3>
    <form action="'
.$this->pi_getPageLink($GLOBALS['TSFE']->id).'" method="POST">
        <input type="text" name="'
.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars['input_field']).'">
        <input type="submit" name="'
.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL('submit_button_label')).'">
    </form>
    <br />
    <p>You can click here to '
.$this->pi_linkToPage('get to this page again',$GLOBALS['TSFE']->id).'</p>
'
;

This form is replaced by:

$content = '
    <h3>Send SMS</h3>
    <form action="'
. $this->pi_getPageLink($GLOBALS['TSFE']->id) . '" method="POST">
        <label>Recipients:</label>
        <br/>
        <input type="text" name="'
. $this->prefixId.'[recipients]" value="' . htmlspecialchars($this->piVars['recipients']) . '">
        <br />
        <label>Text:</label>
        <br />
        <textarea name="'
. $this->prefixId . '[text]">' . htmlspecialchars($this->piVars['text']) . '</textarea>
        <br />
        <input type="submit" name="'
. $this->prefixId . '[submit_button]" value="Send!">
    </form>
'
;

A Send SMS client must then be created using the Developer Center access data:

$client = new SendSmsClient('production', 'Developer Center username', 'Developer Center password');

The first parameter is for the development environment and can only have three denotations: “production,” “sandbox” or “mock.” The second and third parameters are the Developer Center log-in and password.

After the client has been created, the sendSms($recipients, $message, $originator, $flash, $account) function must be invoked.

if (isset($this->piVars['submit_button'])) {
    $client = new SendSmsClient('production', 'Developer Center username', 'Developer Center password');
    $sendSmsResponse = NULL;
    try {
        $sendSmsResponse = $client->sendSms($this->piVars['recipients'], $this->piVars['text'], 'T3Extension', 'false', NULL);
        if ($sendSmsResponse->getStatus()->getStatusConstant() == SendSmsStatusConstants::SUCCESS) {
            $content = 'Deine SMS wurde erfolgreich gesendet' . $content;
        } else {
            $errorMessage = 'The invocation of sendSms() was not successful<br />' .
                'The error code is: ' . $sendSmsResponse->getStatus()->getStatusCode() . '<br/>' .
                'The error message is: ' . $sendSmsResponse->getStatus()->getStatusMessage() . '<br/>';
            throw new Exception($errorMessage);
        }
    } catch(Exception $e) {
        $content = $e->getMessage() . $content;
    }
}

Sending text messages from your own PHP scripts is that simple.

4. Examples of use

Of course, using the Send SMS API is not limited to the sending of simple text messages. The API also lets you send appointment reminders or password reset codes, all sorts of confirmations (e.g., when making online purchases) and much more. A back-end module can also be used to notify administrators of malfunctions or info updates via text message.

This extension is available in the TYPO3 Extensions Repository and in the SVN Repository in TYPO3 Forge.

For more information on the Send SMS API, please visit:
http://www.developergarden.com/apis/apis-sdks/send-sms/

By: Alexander Kraskov

Responses to “How to: Develop Typo3 extensions with the Developer Garden Send SMS API”

    No comments
Leave a Comment