Permalink
Browse files

Add TextMagic API Commands

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information...
1 parent a79aaee commit 242495182b94cb20e02f9bfc10ecf9c24c4c6071 @appleboy committed May 1, 2012
Showing with 216 additions and 18 deletions.
  1. +57 −3 controllers/example.php
  2. +159 −15 libraries/textmagic.php
View
@@ -5,20 +5,74 @@ class Example extends CI_Controller {
public function __construct()
{
parent::__construct();
+ // load library
+ $this->load->library('textmagic');
}
public function index()
{
- // load library
- $this->load->library('textmagic');
+ // send SMS command
+ // $this->send();
+
+ // get credit balance
+ // $this->get_balance();
+
+ // retrieve SMS delivery notification
+ // $this->get_message_status();
+
+ // receive incoming messages
+ // $this->receive();
+
+ // delete incoming messages
+ // $this->delete_reply();
+ // check phone number
+ // $this->check_number();
+ }
+
+ public function send()
+ {
// set text
$text = 'Hi, I am appleboy';
// set phone (string or array)
- $phone = array('xxxxxxxxxx');
+ $phone = array('xxxxxxx', 'xxxxxxx');
$response = $this->textmagic->send($text, $phone);
print_r($response);
}
+
+ public function get_balance()
+ {
+ $response = $this->textmagic->get_balance();
+ print_r($response);
+ }
+
+ public function get_message_status()
+ {
+ $ids = array('xxxxxxx', 'xxxxxxx');
+ $response = $this->textmagic->get_message_status($ids);
+ print_r($response);
+ }
+
+ public function receive()
+ {
+ $ids = 'xxxxxxx';
+ $response = $this->textmagic->receive($ids);
+ print_r($response);
+ }
+
+ public function delete_reply()
+ {
+ $ids = array('xxxxxxx', 'xxxxxxx');
+ $response = $this->textmagic->delete_reply($ids);
+ print_r($response);
+ }
+
+ public function check_number()
+ {
+ $phones = array('xxxxxxx', 'xxxxxxx');
+ $response = $this->textmagic->check_number($phones);
+ print_r($response);
+ }
}
/* End of file example.php */
/* Location: ./application/controllers/example.php */
View
@@ -1,14 +1,15 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
- * TextMagic API Library
+ * TextMagic SMS API wrapper (CodeIgniter Library)
*
* TextMagic's application programming interface (API) provides the communication link
* between your application and TextMagic’s SMS Gateway.
*
- * URL: http://api.textmagic.com/en
- * Author: Bo-Yi Wu <appleboy.tw@gmail.com>
- * Date: 2012-05-01
+ * @author Bo-Yi Wu <apppleboy.tw@gmail.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
+ * @link https://github.com/appleboy/CodeIgniter-TextMagic-API
+ * @date 2012-05-01
*/
class Textmagic {
@@ -17,6 +18,10 @@ class Textmagic {
* API URL
*/
const http_api_url = 'https://www.textmagic.com/app/api';
+ /**
+ * Const for maximum items quantity count in request parameter
+ */
+ const maximum_ids_per_request = 100;
/*
* codeigniter instance
@@ -90,18 +95,157 @@ public function initialize($config = array())
public function send($text, $phones = array(), $is_unicode = true, $send_time = false)
{
- $phones = (is_array($phones)) ? join(',', $phones) : $phones;
+ $phones = (is_array($phones)) ? implode(',', $phones) : $phones;
$params = array(
- 'cmd' => 'send',
- 'phone' => rawurlencode($phones),
- 'text' => rawurlencode($text),
- 'unicode' => $is_unicode ? 1: 0 ,
- 'max_length' => $this->_config['max_length']
+ 'cmd' => 'send',
+ 'phone' => rawurlencode($phones),
+ 'text' => rawurlencode($text),
+ 'unicode' => $is_unicode ? 1: 0 ,
+ 'max_length' => $this->_config['max_length']
);
- if ($send_time) {
- $params['send_time'] = $send_time;
- }
+ $send_time and $params['send_time'] = $send_time;
+
+ $params = array_merge(array('username' => $this->_config['api_username'], 'password' => $this->_config['api_password']), $params);
+
+ $options = array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_SSL_VERIFYHOST => 1,
+ CURLOPT_SSL_VERIFYPEER => 0
+ );
+
+ return $this->request('post', self::http_api_url, $params, $options);
+ }
+
+ /**
+ * Get account balance
+ *
+ * This command is used to check the current SMS credits balance on your account.
+ *
+ * @return integer
+ */
+ public function get_balance()
+ {
+ $params = array(
+ 'cmd' => 'account',
+ );
+
+ $params = array_merge(array('username' => $this->_config['api_username'], 'password' => $this->_config['api_password']), $params);
+
+ $options = array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_SSL_VERIFYHOST => 1,
+ CURLOPT_SSL_VERIFYPEER => 0
+ );
+
+ return $this->request('post', self::http_api_url, $params, $options);
+ }
+
+ /**
+ * Get message's status
+ *
+ * This command allows you to retrieve the delivery status of
+ * any SMS you have already sent.
+ *
+ * @param array $ids
+ *
+ * @return mixed
+ */
+ public function get_message_status($ids = array())
+ {
+ $ids = (is_array($ids)) ? implode(',', $ids) : $ids;
+ $params = array(
+ 'cmd' => 'message_status',
+ 'ids' => rawurlencode($ids)
+ );
+
+ $params = array_merge(array('username' => $this->_config['api_username'], 'password' => $this->_config['api_password']), $params);
+
+ $options = array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_SSL_VERIFYHOST => 1,
+ CURLOPT_SSL_VERIFYPEER => 0
+ );
+
+ return $this->request('post', self::http_api_url, $params, $options);
+ }
+
+ /**
+ * Receive incoming messages
+ *
+ * This command helps you to retrieve the incoming SMS messages from the server.
+ * When SMS is sent to one of our SMS reply numbers
+ * you can request these messages using this API.
+ *
+ * @param array $last_retrieved_id last retrieved incomming message's id
+ *
+ * @return mixed
+ */
+ public function receive($last_retrieved_id = 0)
+ {
+ $params = array(
+ 'cmd' => 'receive',
+ 'last_retrieved_id' => $last_retrieved_id
+ );
+
+ $params = array_merge(array('username' => $this->_config['api_username'], 'password' => $this->_config['api_password']), $params);
+
+ $options = array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_SSL_VERIFYHOST => 1,
+ CURLOPT_SSL_VERIFYPEER => 0
+ );
+
+ return $this->request('post', self::http_api_url, $params, $options);
+ }
+
+ /**
+ * Delete Incoming message
+ *
+ * This command helps you to delete the incoming SMS messages from the server.
+ * After you have read incoming messages sent to one of our SMS reply numbers
+ * you can delete them, so they won't be shown in receice function anymore
+ * and can decrease unread messages.
+ *
+ * @param array $ids
+ *
+ * @return boolean true in case of success
+ *
+ */
+ public function delete_reply($ids)
+ {
+
+ $ids = (is_array($ids)) ? implode(',', $ids) : $ids;
+ $params = array(
+ 'cmd' => 'delete_reply',
+ 'ids' => rawurlencode($ids)
+ );
+
+ $params = array_merge(array('username' => $this->_config['api_username'], 'password' => $this->_config['api_password']), $params);
+
+ $options = array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_SSL_VERIFYHOST => 1,
+ CURLOPT_SSL_VERIFYPEER => 0
+ );
+
+ return $this->request('post', self::http_api_url, $params, $options);
+ }
+
+ /**
+ * Check phone number availability, direction cost and destination country code.
+ *
+ * @param array $phones
+ *
+ * @return mixed
+ */
+ public function check_number($phones = array())
+ {
+ $phones = (is_array($phones)) ? implode(',', $phones) : $phones;
+ $params = array(
+ 'cmd' => 'check_number',
+ 'phone' => rawurlencode($phones)
+ );
$params = array_merge(array('username' => $this->_config['api_username'], 'password' => $this->_config['api_password']), $params);
@@ -116,7 +260,7 @@ public function send($text, $phones = array(), $is_unicode = true, $send_time =
/**
* request data
- * Connect to Nexmo URL API
+ * Connect to API URL
*
* @param array
* return string
@@ -219,5 +363,5 @@ public function get_http_status()
}
}
-/* End of file nexmo.php */
+/* End of file textmagic.php */
/* Location: ./application/libraries/textmagic.php */

0 comments on commit 2424951

Please sign in to comment.