Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 with 226 additions and 59 deletions.
  1. +226 −59 libraries/nexmo.php
285 libraries/nexmo.php
View
@@ -11,9 +11,19 @@
class Nexmo {
// using https by default
- private $_http_xml_url = 'https://rest.nexmo.com/sms/xml';
- private $_http_json_url = 'https://rest.nexmo.com/sms/json';
-
+ const http_xml_url = 'https://rest.nexmo.com/sms/xml';
+ const http_json_url = 'https://rest.nexmo.com/sms/json';
+ public static $balance_url = 'http://rest.nexmo.com/account/get-balance';
+ public static $pricing_url = 'http://rest.nexmo.com/account/get-pricing/outbound';
+ public static $account_url = 'http://rest.nexmo.com/account/settings';
+ public static $number_url = 'http://rest.nexmo.com/account/numbers';
+ public static $search_url = 'http://rest.nexmo.com/number/search';
+ public static $buy_url = 'http://rest.nexmo.com/number/buy';
+ public static $cancel_url = 'http://rest.nexmo.com/number/cancel';
+
+ private $_url_array = array('balance_url', 'pricing_url', 'account_url',
+ 'number_url', 'search_url', 'buy_url', 'cancel_url');
+
// codeigniter instance
private $_ci;
@@ -27,6 +37,10 @@ class Nexmo {
// http reponse
private $_http_status;
private $_http_response;
+
+ protected $session;
+ protected $options = array();
+ protected $url;
function __construct()
{
@@ -34,6 +48,20 @@ function __construct()
$this->_ci->load->config('nexmo');
$this->_api_key = $this->_ci->config->item("api_key");
$this->_api_secret = $this->_ci->config->item("api_secret");
+
+ $this->_initial();
+ }
+
+ /**
+ * initial api url
+ * return null
+ */
+ private function _initial()
+ {
+ foreach($this->_url_array as $key)
+ {
+ self::$$key = self::$$key . '/' . $this->_api_key . '/' . $this->_api_secret;
+ }
}
/**
@@ -81,79 +109,212 @@ public function send_message($from, $to, $message, $type = 'text')
);
$post = array_merge($post, $data);
- return $this->request($post);
+ $params = array_merge(array('username' => $this->_api_key, 'password' => $this->_api_secret), $post);
+ $url = ($this->_format == 'json') ? self::http_json_url : self::http_xml_url;
+
+ $options = array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_SSL_VERIFYHOST => 1,
+ CURLOPT_SSL_VERIFYPEER => 0,
+ CURLOPT_RETURNTRANSFER => TRUE
+ );
+
+ return $this->request('post', $url, $params, $options);
}
/**
- * request data
- * Connect to Nexmo URL API
+ * get_balance
+ * Retrieve your current account balance.
*
- * @param array
* return string
*/
- function request($data = array())
+ function get_balance()
{
- $data = array_merge(array('username' => $this->_api_key, 'password' => $this->_api_secret), $data);
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
+ );
+ return $this->request('get', self::$balance_url, NULL, $options);
+ }
+
+ /**
+ * Account - Get Pricing
+ * Retrieve our outbound pricing for a given country.
+ *
+ * return string
+ */
+ function get_pricing($country_code = 'TW')
+ {
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
+ );
+
+ self::$pricing_url = self::$pricing_url . '/' . $country_code;
+ return $this->request('get', self::$pricing_url, NULL, $options);
+ }
- $data = http_build_query($data);
+ /**
+ * Account - Settings
+ * Update your account settings.
+ *
+ * return string
+ */
+ function get_account_settings($newSecret = NULL, $moCallBackUrl = NULL, $drCallBackUrl = NULL)
+ {
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format),
+ CURLOPT_POST => TRUE,
+ CURLOPT_RETURNTRANSFER => TRUE
+ );
+
+ if(isset($newSecret))
+ $params['newSecret'] = $newSecret;
+ if(isset($moCallBackUrl))
+ $params['moCallBackUrl'] = urlencode($moCallBackUrl);
+ if(isset($drCallBackUrl))
+ $params['drCallBackUrl'] = urlencode($drCallBackUrl);
+
+ return $this->request('post', self::$account_url, $params, $options);
+ }
+
+ /**
+ * Account - Numbers
+ * Get all inbound numbers associated with your Nexmo account.
+ *
+ * return string
+ */
+ function get_numbers()
+ {
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
+ );
+
+ return $this->request('get', self::$number_url, NULL, $options);
+ }
+
+ /**
+ * Number - Search
+ * Get available inbound numbers for a given country.
+ *
+ * return string
+ */
+ function get_number_search($country_code = 'TW', $pattern = NULL)
+ {
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format)
+ );
+
+ self::$search_url = self::$search_url . '/' . $country_code;
- $url = ($this->_format == 'json') ? $this->_http_json_url : $this->_http_xml_url;
+ if(isset($pattern))
+ {
+ $params = array(
+ "pattern" => $params
+ );
+ self::$search_url = self::$search_url . '?' . http_build_query($params);
+ }
- if (function_exists('curl_version'))
+ return $this->request('get', self::$search_url, NULL, $options);
+ }
+
+ /**
+ * Number - Buy
+ * Purchase a given inbound number.
+ *
+ * return string
+ */
+ function get_number_buy($country_code = 'TW', $msisdn = NULL)
+ {
+ if (!isset($msisdn))
{
- $ch = curl_init();
-
- /* POST Url */
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
- /* WARNING: this would prevent curl from detecting a 'man in the middle' attack */
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
- $result = curl_exec($ch);
-
- // show error message
- if($this->_enable_debug)
- {
- if(!curl_errno($ch))
- {
- $info = curl_getinfo($ch);
- echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'] . "<br />";
- }
- else
- {
- echo 'Curl error: ' . curl_error($ch) . "<br />";
- }
- }
-
- $this->_http_response = $result;
- $this->_http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
- curl_close($ch);
+ echo('msisdn must be required');
+ exit();
}
- else if (ini_get('allow_url_fopen'))
+
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format),
+ CURLOPT_POST => TRUE,
+ CURLOPT_RETURNTRANSFER => TRUE
+ );
+
+ self::$buy_url = self::$buy_url . '/' . $country_code . '/' . $msisdn;
+
+ return $this->request('post', self::$buy_url, NULL, $options);
+ }
+
+ /**
+ * Number - Cancel
+ * Cancel a given inbound number subscription.
+ *
+ * return string
+ */
+ function get_number_cancel($country_code = 'TW', $msisdn = NULL)
+ {
+ if (!isset($msisdn))
{
- $opts = array('http' =>
- array(
- 'method' => 'POST',
- 'header' => 'Content-type: application/x-www-form-urlencoded',
- 'content' => $data
- )
- );
- $context = stream_context_create($opts);
- $result = file_get_contents($url, false, $context);
+ echo('msisdn must be required');
+ exit();
+ }
+ $options = array(
+ CURLOPT_HTTPHEADER => array("Accept: application/" . $this->_format),
+ CURLOPT_POST => TRUE,
+ CURLOPT_RETURNTRANSFER => TRUE
+ );
+
+ self::$cancel_url = self::$cancel_url . '/' . $country_code . '/' . $msisdn;
- // get http response code
- preg_match('/.*\s(\d+)\s(.*)$/', $http_response_header[0], $matches);
- $this->_http_status = $matches[1];
+ return $this->request('post', self::$cancel_url, NULL, $options);
+ }
- $this->_http_response = $result;
+ /**
+ * request data
+ * Connect to Nexmo URL API
+ *
+ * @param array
+ * return string
+ */
+ function request($method, $url, $params = array(), $options = array())
+ {
+ if ($method === 'get')
+ {
+ // If a URL is provided, create new session
+ $this->create($url . ($params ? '?' . http_build_query($params) : ''));
}
+ else
+ {
+ $data = $params ? http_build_query($params) : '';
+ $this->create($url);
+
+ $options[CURLOPT_POSTFIELDS] = $data;
+
+ }
+ $this->options($options);
+
+ return $this->execute();
+ }
+
+ public function options($options = array())
+ {
+ // Set all options provided
+ curl_setopt_array($this->session, $options);
+
+ return $this;
+ }
+
+ public function create($url)
+ {
+ $this->url = $url;
+ $this->session = curl_init($this->url);
+ return $this;
+ }
+ public function execute()
+ {
+ // Execute the request & and hide all output
+ $this->_http_response = curl_exec($this->session);
+
+ $this->_http_status = curl_getinfo($this->session, CURLINFO_HTTP_CODE);
+
+ curl_close($this->session);
return $this->response();
}
@@ -230,6 +391,12 @@ public function d_dump($msg)
var_dump($msg);
echo '</pre>';
}
+
+ public function debug()
+ {
+ echo '<br />';
+ echo '<p>url: ' . $this->url . '</p>';
+ }
}
/* End of file nexmo.php */
Please sign in to comment.
Something went wrong with that request. Please try again.