Discussions

Ask a Question
Back to All

Php Support

How to use Till Api in php to send Text Messages

Admin

Thank you for your question.

Sending SMS via Till an be achieved different ways depending on your PHP version and the extensions available in your environment.

If you are using >= PHP 5.5 you can leverage the latest version of GuzzleHTTP (https://github.com/guzzle/guzzle).

Here's an example of sending a text message via Till using GuzzleHTTP:

<?php
// # Example Dependencies
// - PHP >= 5.5.36
// - PHP cURL extension
//   * sudo apt-get install php-curl
// - Composer
//   * curl -sS https://getcomposer.org/installer | php
//
// # Install Guzzle HTTP Client
// - php composer.phar require guzzlehttp/guzzle

// Init Composer
require "vendor/autoload.php";

// Load Guzzle HTTP Client
use Guzzle\Http\Client;

// Your Till credentials
$till_username = "username";
$till_api_key = "api_key";

// The Till project body
$till_project = [
    "phone" => ["phone_number"],
    "text" => "Hello Till!"
];

// Execute HTTP request
$client = new GuzzleHttp\Client();
try {

    $res = $client->request(
        "POST", 
        "https://platform.tillmobile.com/api/send?username=".$till_username."&api_key=".$till_api_key,
        ["body" => json_encode($till_project)]
    );

    // Till HTTP response body
    echo $res->getBody();

} catch(Exception $e) {

    echo $e;

}
?>

If you are on an older version of PHP it may be necessary to use an older version of GuzzleHTTP or possible use the cURL extension directly:
http://php.net/manual/en/curl.examples.php

Note: The json_encode function was introduced in PHP 5.2. If you are using PHP 5.1 you would also need to implement or include a JSON serialization library.

Marked as answered by Nicholas Crafford