You Are Here: Home » Articles » A CakePHP Component for the Servage SMS API

A CakePHP Component for the Servage SMS API

Say it with an automated text message.

Tagged with Servage, SMS, CakePHP and Web Development
Posted on 4/6/07 by Paul Herron

Being a Servage customer, I'm using its SMS API, which offers two main features:
  • Check if a text message can be sent to a specified number.
  • Send a text message to a specified number.

Servage provides some example code for doing this, which I've rewritten as a CakePHP component.

app/controllers/components/sms.php

  1. <?php
  2. class SmsComponent extends Object {
  3.    
  4.     // The Servage URL for posting messages.
  5.     var $send_url = 'http://smsgateway.servage.net/sms.php';
  6.     // The Servage URL for requesting coverage information.
  7.     var $coverage_url = 'http://smsgateway.servage.net/sms_coverage.php';
  8.     // Customer ID.
  9.     var $customer = '00000';
  10.     // Customer key.
  11.     var $key = '00000000';

  12. /**
  13.  * Sends an SMS message to the specified number.
  14.  *
  15.  * @param int $to The phone number to which the message should be sent.
  16.  * @param mixed $message The message to be sent.
  17.  * @param mixed $from The name or number of the sender.
  18.  * @param int $concat The number of messages to join when sending.
  19.  * @return boolean True if the message was successfully posted to Servage.
  20.  */
  21.     function send($to,$message,$from = null,$concat = 1) {
  22.         // Add the parameters to the request URL.
  23.         $request = $this->send_url.'?customer='.$this->customer.'&key='.$this->key.'&number='.urlencode($to).'&message='.urlencode($message).'&concat='.$concat;
  24.         if (!is_null($from))
  25.             $request .= '&from=' . $from;
  26.         // Make the request.
  27.         $ch = curl_init();
  28.         curl_setopt($ch, CURLOPT_URL, $request);
  29.         curl_setopt($ch, CURLOPT_HEADER, 0);
  30.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  31.         $response = curl_exec($ch);
  32.         curl_close($ch);
  33.         // Get the status from the response string.
  34.         $result = split(',',$response);           
  35.         if ($result[0] == 'OK')
  36.             return true;
  37.     }
  38.    
  39. /**
  40.  * Checks if coverage can be provided for the specified number.
  41.  *
  42.  * @param int $number The phone number with international dialling code, e.g. 447796544789.
  43.  * @return boolean True if the specified number has coverage.
  44.  */
  45.     function hasCoverage($number) {
  46.         $ch = curl_init();
  47.         curl_setopt($ch, CURLOPT_URL, $this->coverage_url . '?number='.$number);
  48.         curl_setopt($ch, CURLOPT_HEADER, 0);
  49.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  50.         $response = curl_exec($ch);
  51.         curl_close($ch);       
  52.         $result = split(',',$response);   
  53.         if ($result[0] == 'OK')
  54.             return true;
  55.     }

  56. }
  57. ?>

Once the component is declared in the $components array of the controller, it can be used like this:

  1. // Check coverage.
  2. $this->Sms->hasCoverage('447736044651');
  3. // Send a message.
  4. $this->Sms->send('447736044651', 'This is a test message.'));
  5. // Send a message, this time specifying the sender and span.
  6. $this->Sms->send('447736044651', 'This is a second test message.', 'graemegarden', 2));

Comments

Paul Herron wrote on 12/7/07:

Test

Leave a Comment

*
*
*

« Back to Articles

Article Tags

Show all articles, or just those tagged as:

Apache (1)
CakePHP (5)
Domains (1)
Ethics and That (1)
Freeware (1)
Open Source (1)
Servage (1)
SMS (1)
Software (1)
WAMP (1)
Web Development (6)
Windows (2)

Feed

The articles RSS feed is available here.

Elsewhom

See More…

Back to top.