diff --git a/examples/Account/check-balance.php b/examples/Account/check-balance.php new file mode 100644 index 0000000..eedeb85 --- /dev/null +++ b/examples/Account/check-balance.php @@ -0,0 +1,21 @@ +getAccountBalance(); + +//echo $response["response_code"]; +//echo $response["response_body"]; + +$response_body = json_decode($response["response_body"]); +$available = $response_body->entity->available; +$currency = $response_body->entity->currency; + +echo sprintf("You have a balance of %s %s ", $available, $currency); diff --git a/examples/Lookup/do-lookup.php b/examples/Lookup/do-lookup.php new file mode 100644 index 0000000..9e02b29 --- /dev/null +++ b/examples/Lookup/do-lookup.php @@ -0,0 +1,14 @@ +doLookup("+34666000000"); + +echo $response["response_code"]; +echo $response["response_body"]; diff --git a/examples/Sms/send-sms.php b/examples/Sms/send-sms.php index 8feda20..12e1bc8 100644 --- a/examples/Sms/send-sms.php +++ b/examples/Sms/send-sms.php @@ -4,6 +4,7 @@ require __DIR__ . '/../../vendor/autoload.php'; // Use below for direct download installation +//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php'); //require_once(__DIR__ . '/../../src/Instasent/SmsClient.php'); $instasentClient = new Instasent\SmsClient("my-token"); diff --git a/examples/Sms/sms-info.php b/examples/Sms/sms-info.php index e0f492c..52f5549 100644 --- a/examples/Sms/sms-info.php +++ b/examples/Sms/sms-info.php @@ -4,6 +4,7 @@ require __DIR__ . '/../../vendor/autoload.php'; // Use below for direct download installation +//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php'); //require_once(__DIR__ . '/../../src/Instasent/SmsClient.php'); $instasentClient = new Instasent\SmsClient("my-token"); diff --git a/examples/Verify/check-verify.php b/examples/Verify/check-verify.php index ce0f683..280e9c7 100644 --- a/examples/Verify/check-verify.php +++ b/examples/Verify/check-verify.php @@ -4,6 +4,7 @@ require __DIR__ . '/../../vendor/autoload.php'; // Use below for direct download installation +//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php'); //require_once(__DIR__ . '/../../src/Instasent/VerifyClient.php'); $instasentClient = new Instasent\VerifyClient("my-token"); diff --git a/examples/Verify/request-verify.php b/examples/Verify/request-verify.php index de72e96..534062c 100644 --- a/examples/Verify/request-verify.php +++ b/examples/Verify/request-verify.php @@ -4,6 +4,7 @@ require __DIR__ . '/../../vendor/autoload.php'; // Use below for direct download installation +//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php'); //require_once(__DIR__ . '/../../src/Instasent/VerifyClient.php'); $instasentClient = new Instasent\VerifyClient("my-token"); diff --git a/examples/Verify/verify-info.php b/examples/Verify/verify-info.php index d15bc71..99eacfe 100644 --- a/examples/Verify/verify-info.php +++ b/examples/Verify/verify-info.php @@ -4,7 +4,8 @@ require __DIR__ . '/../../vendor/autoload.php'; // Use below for direct download installation -//require_once(__DIR__ . '/../../src/Instasent/SmsClient.php'); +//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php'); +//require_once(__DIR__ . '/../../src/Instasent/VerifyClient.php'); $instasentClient = new Instasent\VerifyClient("my-token"); $response = $instasentClient->getVerifyById("verifyId"); diff --git a/src/Instasent/Abstracts/InstasentClient.php b/src/Instasent/Abstracts/InstasentClient.php new file mode 100644 index 0000000..9827619 --- /dev/null +++ b/src/Instasent/Abstracts/InstasentClient.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Instasent\Abstracts; + +abstract class InstasentClient +{ + /** + * Endpoint URL + * + * @var string + */ + protected $rootEndpoint = 'http://api.instasent.com'; + + /** + * Secure Channel URL + * + * @var string + */ + protected $secureChannel = 'https://api.instasent.com'; + + /** + * Api Token + * + * @var string + */ + protected $token; + + /** + * Use secure channel flag + * + * @var boolean + */ + protected $useSecureChannel = true; + + /** + * InstasentClient constructor. + * + * @param $token + * @param bool $useSecureChannel + */ + public function __construct($token, $useSecureChannel = true) + { + $this->token = $token; + $this->useSecureChannel = $useSecureChannel; + } + + /** + * Execute the request using curl + * + * @param string $url + * @param string $httpMethod + * @param string $data + * + * @return array + */ + protected function execRequest($url, $httpMethod, $data) + { + $curl = curl_init(); + $headers = array( + 'Authorization: Bearer '.$this->token, + ); + + curl_setopt($curl,CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + + if ($httpMethod == 'POST') { + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); + } + $body = curl_exec($curl); + $info = curl_getinfo($curl); + + return array( + "response_code" => $info['http_code'], + "response_body" => $body, + ); + } +} diff --git a/src/Instasent/AccountClient.php b/src/Instasent/AccountClient.php new file mode 100644 index 0000000..9f5d266 --- /dev/null +++ b/src/Instasent/AccountClient.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Instasent; + +use Instasent\Abstracts\InstasentClient; + +class AccountClient extends InstasentClient +{ + + /** + * Get Account Balance + * + * @return array + */ + public function getAccountBalance() + { + $url = ($this->useSecureChannel) ? $this->secureChannel.'/organization/account/' : $this->rootEndpoint.'/organization/account/'; + $httpMethod = 'GET'; + return $this->execRequest($url, $httpMethod, array()); + } + +} diff --git a/src/Instasent/LookupClient.php b/src/Instasent/LookupClient.php new file mode 100644 index 0000000..a20f2fc --- /dev/null +++ b/src/Instasent/LookupClient.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Instasent; + +use Instasent\Abstracts\InstasentClient; + +class LookupClient extends InstasentClient +{ + + /** + * Do a lookup to get number info + * @param string $to Recipient where lookup is requested + * + * @return array + */ + public function doLookup($to) + { + $url = ($this->useSecureChannel) ? $this->secureChannel.'/lookup/' : $this->rootEndpoint.'/lookup/'; + $httpMethod = 'POST'; + $data = array('to' => $to); + + return $this->execRequest($url, $httpMethod, $data); + } + + /** + * Get a lookup by id + * @param string $id + * + * @return array + */ + public function getLookupById($id) + { + $url = ($this->useSecureChannel) ? $this->secureChannel.'/lookup/'.$id : $this->rootEndpoint.'/lookup/'.$id; + $httpMethod = 'GET'; + return $this->execRequest($url, $httpMethod, array()); + } + + /** + * Get all lookups. Filter by page and resultes per page. + * @param integer $page + * @param integer $perPage + * + * @return array + */ + public function getLookups($page = 1, $perPage = 10) + { + $query = http_build_query(array('page' => $page, 'per_page' => $perPage)); + $url = ($this->useSecureChannel) ? $this->secureChannel.'/lookup/?'.$query : $this->rootEndpoint.'/lookup/?'.$query; + $httpMethod = 'GET'; + return $this->execRequest($url, $httpMethod, array()); + } +} diff --git a/src/Instasent/SmsClient.php b/src/Instasent/SmsClient.php index c6eec8f..7d08b91 100644 --- a/src/Instasent/SmsClient.php +++ b/src/Instasent/SmsClient.php @@ -11,50 +11,19 @@ namespace Instasent; -class SmsClient -{ - /** - * [$rootEndpoint description] - * @var string - */ - private $rootEndpoint = 'http://api.instasent.com'; +use Instasent\Abstracts\InstasentClient; - /** - * [$secureCahnnel description] - * @var string - */ - private $secureChannel = 'https://api.instasent.com'; - - /** - * [$token description] - * @var string - */ - private $token; - - /** - * [$useSecureChannel description] - * @var boolean - */ - private $useSecureChannel = true; - - /** - * [__construct description] - * @param [type] $environment [description] - */ - public function __construct($token, $useSecureChannel = true) - { - $this->token = $token; - $this->useSecureChannel = $useSecureChannel; - } +class SmsClient extends InstasentClient +{ /** - * [sendSms Send a sms.] - * @param [string] $from [Remittent, 11chars max] - * @param [string] $to [Recipient where SMS is delivered, Include the country phone prefix format E164] - * @param [string] $text [Message text content, 160 chars per SMS] - * @param [string] $clientId [An user reference for your internal use, Optional - 40chars max, Unique per SMS] + * Send a sms + * @param string $from Remittent, 11chars max + * @param string $to Recipient where SMS is delivered, Include the country phone prefix format E164 + * @param string $text Message text content, 160 chars per SMS + * @param string $clientId An user reference for your internal use, Optional - 40chars max, Unique per SMS * - * @return array [description] + * @return array */ public function sendSms($from, $to, $text, $clientId = null) { @@ -68,10 +37,10 @@ public function sendSms($from, $to, $text, $clientId = null) } /** - * [getSms Get Sms by entity Id.] - * @param [type] $id [description] + * Get Sms by entity Id + * @param string $id * - * @return array [description] + * @return array */ public function getSmsById($id) { @@ -81,11 +50,11 @@ public function getSmsById($id) } /** - * [getSms Get all sms. Filter by page and resultes per page.] - * @param integer $page [description] - * @param integer $perPage [description] - - * @return array [description] + * Get all sms. Filter by page and resultes per page. + * @param integer $page + * @param integer $perPage + * + * @return array */ public function getSms($page = 1, $perPage = 10) { @@ -94,36 +63,4 @@ public function getSms($page = 1, $perPage = 10) $httpMethod = 'GET'; return $this->execRequest($url, $httpMethod, array()); } - - /** - * [execRequest Execute the request using curl] - * @param [type] $url [description] - * @param [type] $httpMethod [description] - * @param [type] $data [description] - * - * @return array [description] - */ - private function execRequest($url, $httpMethod, $data) - { - $curl = curl_init(); - $headers = array( - 'Authorization: Bearer '.$this->token, - ); - - curl_setopt($curl,CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - - if ($httpMethod == 'POST') { - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); - } - $body = curl_exec($curl); - $info = curl_getinfo($curl); - - return array( - "response_code" => $info['http_code'], - "response_body" => $body, - ); - } } diff --git a/src/Instasent/VerifyClient.php b/src/Instasent/VerifyClient.php index e6115d0..12c4f5b 100644 --- a/src/Instasent/VerifyClient.php +++ b/src/Instasent/VerifyClient.php @@ -11,52 +11,21 @@ namespace Instasent; -class VerifyClient -{ - /** - * [$rootEndpoint description] - * @var string - */ - private $rootEndpoint = 'http://api.instasent.com'; - - /** - * [$secureCahnnel description] - * @var string - */ - private $secureChannel = 'https://api.instasent.com'; - - /** - * [$token description] - * @var string - */ - private $token; - - /** - * [$useSecureChannel description] - * @var boolean - */ - private $useSecureChannel = true; - - /** - * [__construct description] - * @param [type] $environment [description] - */ - public function __construct($token, $useSecureChannel = true) - { - $this->token = $token; - $this->useSecureChannel = $useSecureChannel; - } +use Instasent\Abstracts\InstasentClient; +class VerifyClient extends InstasentClient +{ /** - * [request a Verify for a number.] - * @param [string] $from [Remittent, 11chars max] - * @param [string] $to [Recipient where SMS is delivered, Include the country phone prefix format E164] - * @param [string] $text [Message text content, 160 chars per SMS] - * @param [string] $tokenLength [Length of token code. min => 3, max => 8] - * @param [string] $timeout [Time token is valid in seconds. min => 30] - * @param [string] $clientId [An user reference for your internal use, Optional - 40chars max, Unique per SMS] + * Request a Verify for a number * - * @return array [description] + * @param string $from Remittent, 11chars max + * @param string $to Recipient where SMS is delivered, Include the country phone prefix format E164 + * @param string $text Message text content, 160 chars per SMS + * @param string $tokenLength Length of token code. min => 3, max => 8 + * @param string $timeout Time token is valid in seconds. min => 30 + * @param string $clientId An user reference for your internal use, Optional - 40chars max, Unique per Verify + * + * @return array */ public function requestVerify($from, $to, $text, $tokenLength = null, $timeout = null, $clientId = null) { @@ -80,11 +49,12 @@ public function requestVerify($from, $to, $text, $tokenLength = null, $timeout = } /** - * [getVerify Get Verify by entity Id.] - * @param [string] $id [verify id] - * @param [string] $token [token value] + * Check a Verify by entity Id and token + * + * @param string $id + * @param string $token * - * @return array [description] + * @return array */ public function checkVerify($id, $token) { @@ -95,10 +65,10 @@ public function checkVerify($id, $token) } /** - * [getVerify Get Verify by entity Id.] - * @param [type] $id [description] + * Get Verify by entity Id + * @param string $id * - * @return array [description] + * @return array */ public function getVerifyById($id) { @@ -108,11 +78,11 @@ public function getVerifyById($id) } /** - * [getSms Get all sms. Filter by page and resultes per page.] - * @param integer $page [description] - * @param integer $perPage [description] - - * @return array [description] + * Get all verifies. Filter by page and resultes per page. + * @param integer $page + * @param integer $perPage + * + * @return array */ public function getVerify($page = 1, $perPage = 10) { @@ -122,35 +92,4 @@ public function getVerify($page = 1, $perPage = 10) return $this->execRequest($url, $httpMethod, array()); } - /** - * [execRequest Execute the request using curl] - * @param [type] $url [description] - * @param [type] $httpMethod [description] - * @param [type] $data [description] - * - * @return array [description] - */ - private function execRequest($url, $httpMethod, $data) - { - $curl = curl_init(); - $headers = array( - 'Authorization: Bearer '.$this->token, - ); - - curl_setopt($curl,CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - - if ($httpMethod == 'POST') { - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); - } - $body = curl_exec($curl); - $info = curl_getinfo($curl); - - return array( - "response_code" => $info['http_code'], - "response_body" => $body, - ); - } }