Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add the following API:

- Account: Get Balance
- Account: Get Pricing
- Account: Settings
- Account: Numbers
- Number: Search
- Number: Buy
- Number: Cancel

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information...
commit f76ea9a01ae5b00d81d6b6239974f54ebca4af74 1 parent 57ddbb5
Bo-Yi Wu authored

Showing 1 changed file with 226 additions and 59 deletions. Show diff stats Hide diff stats

  1. +226 59 libraries/nexmo.php
285 libraries/nexmo.php
@@ -11,9 +11,19 @@
11 11 class Nexmo {
12 12
13 13 // using https by default
14   - private $_http_xml_url = 'https://rest.nexmo.com/sms/xml';
15   - private $_http_json_url = 'https://rest.nexmo.com/sms/json';
16   -
  14 + const http_xml_url = 'https://rest.nexmo.com/sms/xml';
  15 + const http_json_url = 'https://rest.nexmo.com/sms/json';
  16 + public static $balance_url = 'http://rest.nexmo.com/account/get-balance';
  17 + public static $pricing_url = 'http://rest.nexmo.com/account/get-pricing/outbound';
  18 + public static $account_url = 'http://rest.nexmo.com/account/settings';
  19 + public static $number_url = 'http://rest.nexmo.com/account/numbers';
  20 + public static $search_url = 'http://rest.nexmo.com/number/search';
  21 + public static $buy_url = 'http://rest.nexmo.com/number/buy';
  22 + public static $cancel_url = 'http://rest.nexmo.com/number/cancel';
  23 +
  24 + private $_url_array = array('balance_url', 'pricing_url', 'account_url',
  25 + 'number_url', 'search_url', 'buy_url', 'cancel_url');
  26 +
17 27 // codeigniter instance
18 28 private $_ci;
19 29
@@ -27,6 +37,10 @@ class Nexmo {
27 37 // http reponse
28 38 private $_http_status;
29 39 private $_http_response;
  40 +
  41 + protected $session;
  42 + protected $options = array();
  43 + protected $url;
30 44
31 45 function __construct()
32 46 {
@@ -34,6 +48,20 @@ function __construct()
34 48 $this->_ci->load->config('nexmo');
35 49 $this->_api_key = $this->_ci->config->item("api_key");
36 50 $this->_api_secret = $this->_ci->config->item("api_secret");
  51 +
  52 + $this->_initial();
  53 + }
  54 +
  55 + /**
  56 + * initial api url
  57 + * return null
  58 + */
  59 + private function _initial()
  60 + {
  61 + foreach($this->_url_array as $key)
  62 + {
  63 + self::$$key = self::$$key . '/' . $this->_api_key . '/' . $this->_api_secret;
  64 + }
37 65 }
38 66
39 67 /**
@@ -81,79 +109,212 @@ public function send_message($from, $to, $message, $type = 'text')
81 109 );
82 110 $post = array_merge($post, $data);
83 111
84   - return $this->request($post);
  112 + $params = array_merge(array('username' => $this->_api_key, 'password' => $this->_api_secret), $post);
  113 + $url = ($this->_format == 'json') ? self::http_json_url : self::http_xml_url;
  114 +
  115 + $options = array(
  116 + CURLOPT_POST => TRUE,
  117 + CURLOPT_SSL_VERIFYHOST => 1,
  118 + CURLOPT_SSL_VERIFYPEER => 0,
  119 + CURLOPT_RETURNTRANSFER => TRUE
  120 + );
  121 +
  122 + return $this->request('post', $url, $params, $options);
85 123 }
86 124
87 125 /**
88   - * request data
89   - * Connect to Nexmo URL API
  126 + * get_balance
  127 + * Retrieve your current account balance.
90 128 *
91   - * @param array
92 129 * return string
93 130 */
94   - function request($data = array())
  131 + function get_balance()
95 132 {
96   - $data = array_merge(array('username' => $this->_api_key, 'password' => $this->_api_secret), $data);
  133 + $options = array(
  134 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
  135 + );
  136 + return $this->request('get', self::$balance_url, NULL, $options);
  137 + }
  138 +
  139 + /**
  140 + * Account - Get Pricing
  141 + * Retrieve our outbound pricing for a given country.
  142 + *
  143 + * return string
  144 + */
  145 + function get_pricing($country_code = 'TW')
  146 + {
  147 + $options = array(
  148 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
  149 + );
  150 +
  151 + self::$pricing_url = self::$pricing_url . '/' . $country_code;
  152 + return $this->request('get', self::$pricing_url, NULL, $options);
  153 + }
97 154
98   - $data = http_build_query($data);
  155 + /**
  156 + * Account - Settings
  157 + * Update your account settings.
  158 + *
  159 + * return string
  160 + */
  161 + function get_account_settings($newSecret = NULL, $moCallBackUrl = NULL, $drCallBackUrl = NULL)
  162 + {
  163 + $options = array(
  164 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format),
  165 + CURLOPT_POST => TRUE,
  166 + CURLOPT_RETURNTRANSFER => TRUE
  167 + );
  168 +
  169 + if(isset($newSecret))
  170 + $params['newSecret'] = $newSecret;
  171 + if(isset($moCallBackUrl))
  172 + $params['moCallBackUrl'] = urlencode($moCallBackUrl);
  173 + if(isset($drCallBackUrl))
  174 + $params['drCallBackUrl'] = urlencode($drCallBackUrl);
  175 +
  176 + return $this->request('post', self::$account_url, $params, $options);
  177 + }
  178 +
  179 + /**
  180 + * Account - Numbers
  181 + * Get all inbound numbers associated with your Nexmo account.
  182 + *
  183 + * return string
  184 + */
  185 + function get_numbers()
  186 + {
  187 + $options = array(
  188 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
  189 + );
  190 +
  191 + return $this->request('get', self::$number_url, NULL, $options);
  192 + }
  193 +
  194 + /**
  195 + * Number - Search
  196 + * Get available inbound numbers for a given country.
  197 + *
  198 + * return string
  199 + */
  200 + function get_number_search($country_code = 'TW', $pattern = NULL)
  201 + {
  202 + $options = array(
  203 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
  204 + );
  205 +
  206 + self::$search_url = self::$search_url . '/' . $country_code;
99 207
100   - $url = ($this->_format == 'json') ? $this->_http_json_url : $this->_http_xml_url;
  208 + if(isset($pattern))
  209 + {
  210 + $params = array(
  211 + "pattern" => $params
  212 + );
  213 + self::$search_url = self::$search_url . '?' . http_build_query($params);
  214 + }
101 215
102   - if (function_exists('curl_version'))
  216 + return $this->request('get', self::$search_url, NULL, $options);
  217 + }
  218 +
  219 + /**
  220 + * Number - Buy
  221 + * Purchase a given inbound number.
  222 + *
  223 + * return string
  224 + */
  225 + function get_number_buy($country_code = 'TW', $msisdn = NULL)
  226 + {
  227 + if (!isset($msisdn))
103 228 {
104   - $ch = curl_init();
105   -
106   - /* POST Url */
107   - curl_setopt($ch, CURLOPT_URL, $url);
108   - curl_setopt($ch, CURLOPT_POST, TRUE);
109   - curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
110   -
111   - /* WARNING: this would prevent curl from detecting a 'man in the middle' attack */
112   - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
113   - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
114   -
115   - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
116   -
117   - $result = curl_exec($ch);
118   -
119   - // show error message
120   - if($this->_enable_debug)
121   - {
122   - if(!curl_errno($ch))
123   - {
124   - $info = curl_getinfo($ch);
125   - echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'] . "<br />";
126   - }
127   - else
128   - {
129   - echo 'Curl error: ' . curl_error($ch) . "<br />";
130   - }
131   - }
132   -
133   - $this->_http_response = $result;
134   - $this->_http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
135   -
136   - curl_close($ch);
  229 + echo('msisdn must be required');
  230 + exit();
137 231 }
138   - else if (ini_get('allow_url_fopen'))
  232 +
  233 + $options = array(
  234 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format),
  235 + CURLOPT_POST => TRUE,
  236 + CURLOPT_RETURNTRANSFER => TRUE
  237 + );
  238 +
  239 + self::$buy_url = self::$buy_url . '/' . $country_code . '/' . $msisdn;
  240 +
  241 + return $this->request('post', self::$buy_url, NULL, $options);
  242 + }
  243 +
  244 + /**
  245 + * Number - Cancel
  246 + * Cancel a given inbound number subscription.
  247 + *
  248 + * return string
  249 + */
  250 + function get_number_cancel($country_code = 'TW', $msisdn = NULL)
  251 + {
  252 + if (!isset($msisdn))
139 253 {
140   - $opts = array('http' =>
141   - array(
142   - 'method' => 'POST',
143   - 'header' => 'Content-type: application/x-www-form-urlencoded',
144   - 'content' => $data
145   - )
146   - );
147   - $context = stream_context_create($opts);
148   - $result = file_get_contents($url, false, $context);
  254 + echo('msisdn must be required');
  255 + exit();
  256 + }
  257 + $options = array(
  258 + CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format),
  259 + CURLOPT_POST => TRUE,
  260 + CURLOPT_RETURNTRANSFER => TRUE
  261 + );
  262 +
  263 + self::$cancel_url = self::$cancel_url . '/' . $country_code . '/' . $msisdn;
149 264
150   - // get http response code
151   - preg_match('/.*\s(\d+)\s(.*)$/', $http_response_header[0], $matches);
152   - $this->_http_status = $matches[1];
  265 + return $this->request('post', self::$cancel_url, NULL, $options);
  266 + }
153 267
154   - $this->_http_response = $result;
  268 + /**
  269 + * request data
  270 + * Connect to Nexmo URL API
  271 + *
  272 + * @param array
  273 + * return string
  274 + */
  275 + function request($method, $url, $params = array(), $options = array())
  276 + {
  277 + if ($method === 'get')
  278 + {
  279 + // If a URL is provided, create new session
  280 + $this->create($url . ($params ? '?' . http_build_query($params) : ''));
155 281 }
  282 + else
  283 + {
  284 + $data = $params ? http_build_query($params) : '';
  285 + $this->create($url);
  286 +
  287 + $options[CURLOPT_POSTFIELDS] = $data;
  288 +
  289 + }
  290 + $this->options($options);
  291 +
  292 + return $this->execute();
  293 + }
  294 +
  295 + public function options($options = array())
  296 + {
  297 + // Set all options provided
  298 + curl_setopt_array($this->session, $options);
  299 +
  300 + return $this;
  301 + }
  302 +
  303 + public function create($url)
  304 + {
  305 + $this->url = $url;
  306 + $this->session = curl_init($this->url);
  307 + return $this;
  308 + }
156 309
  310 + public function execute()
  311 + {
  312 + // Execute the request & and hide all output
  313 + $this->_http_response = curl_exec($this->session);
  314 +
  315 + $this->_http_status = curl_getinfo($this->session, CURLINFO_HTTP_CODE);
  316 +
  317 + curl_close($this->session);
157 318 return $this->response();
158 319 }
159 320
@@ -230,6 +391,12 @@ public function d_dump($msg)
230 391 var_dump($msg);
231 392 echo '</pre>';
232 393 }
  394 +
  395 + public function debug()
  396 + {
  397 + echo '<br />';
  398 + echo '<p>url: ' . $this->url . '</p>';
  399 + }
233 400 }
234 401
235 402 /* End of file nexmo.php */

0 comments on commit f76ea9a

Please sign in to comment.
Something went wrong with that request. Please try again.