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
<?php class SmsComponent extends Object { // The Servage URL for posting messages. var $send_url = 'http://smsgateway.servage.net/sms.php'; // The Servage URL for requesting coverage information. var $coverage_url = 'http://smsgateway.servage.net/sms_coverage.php'; // Customer ID. var $customer = '00000'; // Customer key. var $key = '00000000'; /** * Sends an SMS message to the specified number. * * @param int $to The phone number to which the message should be sent. * @param mixed $message The message to be sent. * @param mixed $from The name or number of the sender. * @param int $concat The number of messages to join when sending. * @return boolean True if the message was successfully posted to Servage. */ function send($to,$message,$from = null,$concat = 1) { // Add the parameters to the request URL. $request .= '&from=' . $from; // Make the request. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); // Get the status from the response string. if ($result[0] == 'OK') return true; } /** * Checks if coverage can be provided for the specified number. * * @param int $number The phone number with international dialling code, e.g. 447796544789. * @return boolean True if the specified number has coverage. */ function hasCoverage($number) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->coverage_url . '?number='.$number); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); if ($result[0] == 'OK') return true; } } ?>
Once the component is declared in the $components array of the controller, it can be used like this:
// Check coverage. $this->Sms->hasCoverage('447736044651'); // Send a message. $this->Sms->send('447736044651', 'This is a test message.')); // Send a message, this time specifying the sender and span. $this->Sms->send('447736044651', 'This is a second test message.', 'graemegarden', 2));
Leave a Comment
Article Tags
Show all articles, or just those tagged as:
- Apache (1)
- CakePHP (10)
- Domains (1)
- Freeware (1)
- Graphics (1)
- Life (1)
- Open Source (2)
- Servage (1)
- SMS (1)
- Software (3)
- Ubuntu (2)
- WAMP (2)
- Web Development (13)
- Windows (2)
- Work (2)
Feed
The articles RSS feed is available.
Elsewhom
-
Ubuntu Blog.
Not quite everything about Ubuntu, but close -
Andy Budd::Blogography.
Web usability chat that's sure to get your pulse racing -
cakebaker.
Baking cakes with CakePHP
Enlightening CakePHP articles -
A List Apart.
Influential webzine for web designers -
hackdiary.
Matt Biddulph's blog
