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

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));

Leave a Comment

CAPTCHA[Refresh]

« Articles

Article Tags

Show all articles, or just those tagged as:

Feed

The articles RSS feed is available.

Elsewhom

See More…

Back to top.