/app/Libs/AfricasTalking.php
PHP | 329 lines | 247 code | 74 blank | 8 comment | 51 complexity | 8a6a71e66d3c627d7bedcad4732be31f MD5 | raw file
- <?php
- namespace App\Libs;
- class AfricasTalking
- {
- protected $_username;
- protected $_apiKey;
- protected $_shortCode;
- protected $_keyWord;
- protected $_requestBody;
- protected $_requestUrl;
- protected $_responseBody;
- protected $_responseInfo;
- const SMS_URL = 'https://api.africastalking.com/version1/messaging';
- const VOICE_URL = 'https://voice.africastalking.com';
- const USER_DATA_URL = 'https://api.africastalking.com/version1/user';
- const SUBSCRIPTION_URL = 'https://api.africastalking.com/version1/subscription';
- const AIRTIME_URL = 'https://api.africastalking.com/version1/airtime';
- //Turn this on if you run into problems. It will print the raw HTTP response from our server
- const Debug = false;
- const HTTP_CODE_OK = 200;
- const HTTP_CODE_CREATED = 201;
- public function __construct()
- {
- $this->_username = config('africastalking.user_name');
- $this->_apiKey = config('africastalking.api_key');
- $this->_shortCode = config('africastalking.short_code');
- $this->_keyWord = config('africastalking.key_word');
- $this->_requestBody = null;
- $this->_requestUrl = null;
- $this->_responseBody = null;
- $this->_responseInfo = null;
- }
- //Messaging methods
- public function sendMessage($to_, $message_, $from_ = null, $bulkSMSMode_ = 1, Array $options_ = array())
- {
- if (strlen($to_) == 0 || strlen($message_) == 0) {
- throw new AfricasTalkingException('Please supply both to and message parameters');
- }
- $params = array(
- 'username' => $this->_username,
- 'to' => $to_,
- 'message' => $message_,
- );
- if ($from_ !== null) {
- $params['from'] = $from_;
- $params['bulkSMSMode'] = $bulkSMSMode_;
- }
- //This contains a list of parameters that can be passed in $options_ parameter
- if (count($options_) > 0) {
- $allowedKeys = array(
- 'enqueue',
- 'keyword',
- 'linkId',
- 'retryDurationInHours'
- );
- //Check whether data has been passed in options_ parameter
- foreach ($options_ as $key => $value) {
- if (in_array($key, $allowedKeys) && strlen($value) > 0) {
- $params[$key] = $value;
- } else {
- throw new AfricasTalkingException("Invalid key in options array: [$key]");
- }
- }
- }
- $this->_requestUrl = self::SMS_URL;
- $this->_requestBody = http_build_query($params, '', '&');
- $this->executePOST();
- if ($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) {
- $responseObject = json_decode($this->_responseBody);
- return $responseObject->SMSMessageData->Recipients;
- }
- throw new AfricasTalkingException($this->_responseBody);
- }
- public function fetchMessages($lastReceivedId_)
- {
- $username = $this->_username;
- $this->_requestUrl = self::SMS_URL . '?username=' . $username . '&lastReceivedId=' . intval($lastReceivedId_);
- $this->executeGet();
- if ($this->_responseInfo['http_code'] == self::HTTP_CODE_OK) {
- $responseObject = json_decode($this->_responseBody);
- return $responseObject->SMSMessageData->Messages;
- }
- throw new AfricasTalkingException($this->_responseBody);
- }
- //Subscription methods
- public function createSubscription($phoneNumber_)
- {
- if (strlen($phoneNumber_) == 0 || strlen($this->_shortCode) == 0 || strlen($this->_keyWord) == 0) {
- throw new AfricasTalkingException('Please supply phoneNumber, shortCode and keyword');
- }
- $params = array(
- 'username' => $this->_username,
- 'phoneNumber' => $phoneNumber_,
- 'shortCode' => $this->_shortCode,
- 'keyword' => $this->_keyWord
- );
- $this->_requestUrl = self::SUBSCRIPTION_URL . "/create";
- $this->_requestBody = http_build_query($params, '', '&');
- $this->executePOST();
- if ($this->_responseInfo['http_code'] != self::HTTP_CODE_CREATED)
- throw new AfricasTalkingException($this->_responseBody);
- return json_decode($this->_responseBody);
- }
- public function deleteSubscription($phoneNumber_)
- {
- if (strlen($phoneNumber_) == 0 || strlen($this->_shortCode) == 0 || strlen($this->_keyWord) == 0) {
- throw new AfricasTalkingException('Please supply phoneNumber, shortCode and keyword');
- }
- $params = array(
- 'username' => $this->_username,
- 'phoneNumber' => $phoneNumber_,
- 'shortCode' => $this->_shortCode,
- 'keyword' => $this->_keyWord
- );
- $this->_requestUrl = self::SUBSCRIPTION_URL . "/delete";
- $this->_requestBody = http_build_query($params, '', '&');
- $this->executePOST();
- if ($this->_responseInfo['http_code'] != self::HTTP_CODE_CREATED)
- throw new AfricasTalkingException($this->_responseBody);
- return json_decode($this->_responseBody);
- }
- public function fetchPremiumSubscriptions($lastReceivedId_ = 0)
- {
- $username = $this->_username;
- $this->_requestUrl = self::SUBSCRIPTION_URL . '?username=' . $username . '&shortCode=' . $this->_shortCode;
- $this->_requestUrl .= '&keyword=' . $this->_keyWord . '&lastReceivedId=' . intval($lastReceivedId_);
- $this->executeGet();
- if ($this->_responseInfo['http_code'] == self::HTTP_CODE_OK) {
- $responseObject = json_decode($this->_responseBody);
- return $responseObject->responses;
- }
- throw new AfricasTalkingException($this->_responseBody);
- }
- //Call methods
- public function call($from_, $to_)
- {
- if (strlen($from_) == 0 || strlen($to_) == 0) {
- throw new AfricasTalkingException('Please supply both from and to parameters');
- }
- $params = array(
- 'username' => $this->_username,
- 'from' => $from_,
- 'to' => $to_
- );
- $this->_requestUrl = self::VOICE_URL . "/call";
- $this->_requestBody = http_build_query($params, '', '&');
- $this->executePOST();
- if (($responseObject = json_decode($this->_responseBody)) !== null) {
- if ($responseObject->Status != "Success")
- throw new AfricasTalkingException($responseObject->ErrorMessage);
- } else
- throw new AfricasTalkingException($this->_responseBody);
- }
- public function getNumQueuedCalls($phoneNumber_, $queueName = null)
- {
- $this->_requestUrl = self::VOICE_URL . "/queueStatus";
- $params = array(
- "username" => $this->_username,
- "phoneNumbers" => $phoneNumber_
- );
- if ($queueName !== null)
- $params['queueName'] = $queueName;
- $this->_requestBody = http_build_query($params, '', '&');
- $this->executePOST();
- if (($responseObject = json_decode($this->_responseBody)) !== null) {
- if ($responseObject->Status == "Success")
- return $responseObject->NumQueued;
- throw new AfricasTalkingException($responseObject->ErrorMessage);
- }
- throw new AfricasTalkingException($this->_responseBody);
- }
- public function uploadMediaFile($url_)
- {
- $params = array(
- "username" => $this->_username,
- "url" => $url_
- );
- $this->_requestBody = http_build_query($params, '', '&');
- $this->_requestUrl = self::VOICE_URL . "/mediaUpload";
- $this->executePOST();
- if (($responseObject = json_decode($this->_responseBody)) !== null) {
- if ($responseObject->Status != "Success")
- throw new AfricasTalkingException($responseObject->ErrorMessage);
- } else
- throw new AfricasTalkingException($this->_responseBody);
- }
- //Airtime method
- public function sendAirtime($recipients)
- {
- $params = array(
- "username" => $this->_username,
- "recipients" => $recipients
- );
- $this->_requestUrl = self::AIRTIME_URL . "/send";
- $this->_requestBody = http_build_query($params, '', '&');
- $this->executePOST();
- if ($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) {
- $responseObject = json_decode($this->_responseBody);
- if (count($responseObject->responses) > 0)
- return $responseObject->responses;
- throw new AfricasTalkingException($responseObject->errorMessage);
- }
- throw new AfricasTalkingException($this->_responseBody);
- }
- //User info method
- public function getUserData()
- {
- $username = $this->_username;
- $this->_requestUrl = self::USER_DATA_URL . '?username=' . $username;
- $this->executeGet();
- if ($this->_responseInfo['http_code'] == self::HTTP_CODE_OK) {
- $responseObject = json_decode($this->_responseBody);
- return $responseObject->UserData;
- }
- throw new AfricasTalkingException($this->_responseBody);
- }
- private function executeGet()
- {
- $ch = curl_init();
- $this->doExecute($ch);
- }
- private function executePost()
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_requestBody);
- curl_setopt($ch, CURLOPT_POST, 1);
- $this->doExecute($ch);
- }
- private function doExecute(&$curlHandle_)
- {
- try {
- $this->setCurlOpts($curlHandle_);
- $responseBody = curl_exec($curlHandle_);
- if (self::Debug) {
- echo "Full response: " . print_r($responseBody, true) . "\n";
- }
- $this->_responseInfo = curl_getinfo($curlHandle_);
- $this->_responseBody = $responseBody;
- curl_close($curlHandle_);
- } catch (Exeption $e) {
- curl_close($curlHandle_);
- throw $e;
- }
- }
- private function setCurlOpts(&$curlHandle_)
- {
- curl_setopt($curlHandle_, CURLOPT_TIMEOUT, 60);
- curl_setopt($curlHandle_, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curlHandle_, CURLOPT_URL, $this->_requestUrl);
- curl_setopt($curlHandle_, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curlHandle_, CURLOPT_HTTPHEADER, array('Accept: application/json',
- 'apikey: ' . $this->_apiKey));
- }
- }
- class AfricasTalkingException extends \Exception{}