PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Libs/AfricasTalking.php

https://gitlab.com/statusdevs/owenv2
PHP | 329 lines | 247 code | 74 blank | 8 comment | 51 complexity | 8a6a71e66d3c627d7bedcad4732be31f MD5 | raw file
  1. <?php
  2. namespace App\Libs;
  3. class AfricasTalking
  4. {
  5. protected $_username;
  6. protected $_apiKey;
  7. protected $_shortCode;
  8. protected $_keyWord;
  9. protected $_requestBody;
  10. protected $_requestUrl;
  11. protected $_responseBody;
  12. protected $_responseInfo;
  13. const SMS_URL = 'https://api.africastalking.com/version1/messaging';
  14. const VOICE_URL = 'https://voice.africastalking.com';
  15. const USER_DATA_URL = 'https://api.africastalking.com/version1/user';
  16. const SUBSCRIPTION_URL = 'https://api.africastalking.com/version1/subscription';
  17. const AIRTIME_URL = 'https://api.africastalking.com/version1/airtime';
  18. //Turn this on if you run into problems. It will print the raw HTTP response from our server
  19. const Debug = false;
  20. const HTTP_CODE_OK = 200;
  21. const HTTP_CODE_CREATED = 201;
  22. public function __construct()
  23. {
  24. $this->_username = config('africastalking.user_name');
  25. $this->_apiKey = config('africastalking.api_key');
  26. $this->_shortCode = config('africastalking.short_code');
  27. $this->_keyWord = config('africastalking.key_word');
  28. $this->_requestBody = null;
  29. $this->_requestUrl = null;
  30. $this->_responseBody = null;
  31. $this->_responseInfo = null;
  32. }
  33. //Messaging methods
  34. public function sendMessage($to_, $message_, $from_ = null, $bulkSMSMode_ = 1, Array $options_ = array())
  35. {
  36. if (strlen($to_) == 0 || strlen($message_) == 0) {
  37. throw new AfricasTalkingException('Please supply both to and message parameters');
  38. }
  39. $params = array(
  40. 'username' => $this->_username,
  41. 'to' => $to_,
  42. 'message' => $message_,
  43. );
  44. if ($from_ !== null) {
  45. $params['from'] = $from_;
  46. $params['bulkSMSMode'] = $bulkSMSMode_;
  47. }
  48. //This contains a list of parameters that can be passed in $options_ parameter
  49. if (count($options_) > 0) {
  50. $allowedKeys = array(
  51. 'enqueue',
  52. 'keyword',
  53. 'linkId',
  54. 'retryDurationInHours'
  55. );
  56. //Check whether data has been passed in options_ parameter
  57. foreach ($options_ as $key => $value) {
  58. if (in_array($key, $allowedKeys) && strlen($value) > 0) {
  59. $params[$key] = $value;
  60. } else {
  61. throw new AfricasTalkingException("Invalid key in options array: [$key]");
  62. }
  63. }
  64. }
  65. $this->_requestUrl = self::SMS_URL;
  66. $this->_requestBody = http_build_query($params, '', '&');
  67. $this->executePOST();
  68. if ($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) {
  69. $responseObject = json_decode($this->_responseBody);
  70. return $responseObject->SMSMessageData->Recipients;
  71. }
  72. throw new AfricasTalkingException($this->_responseBody);
  73. }
  74. public function fetchMessages($lastReceivedId_)
  75. {
  76. $username = $this->_username;
  77. $this->_requestUrl = self::SMS_URL . '?username=' . $username . '&lastReceivedId=' . intval($lastReceivedId_);
  78. $this->executeGet();
  79. if ($this->_responseInfo['http_code'] == self::HTTP_CODE_OK) {
  80. $responseObject = json_decode($this->_responseBody);
  81. return $responseObject->SMSMessageData->Messages;
  82. }
  83. throw new AfricasTalkingException($this->_responseBody);
  84. }
  85. //Subscription methods
  86. public function createSubscription($phoneNumber_)
  87. {
  88. if (strlen($phoneNumber_) == 0 || strlen($this->_shortCode) == 0 || strlen($this->_keyWord) == 0) {
  89. throw new AfricasTalkingException('Please supply phoneNumber, shortCode and keyword');
  90. }
  91. $params = array(
  92. 'username' => $this->_username,
  93. 'phoneNumber' => $phoneNumber_,
  94. 'shortCode' => $this->_shortCode,
  95. 'keyword' => $this->_keyWord
  96. );
  97. $this->_requestUrl = self::SUBSCRIPTION_URL . "/create";
  98. $this->_requestBody = http_build_query($params, '', '&');
  99. $this->executePOST();
  100. if ($this->_responseInfo['http_code'] != self::HTTP_CODE_CREATED)
  101. throw new AfricasTalkingException($this->_responseBody);
  102. return json_decode($this->_responseBody);
  103. }
  104. public function deleteSubscription($phoneNumber_)
  105. {
  106. if (strlen($phoneNumber_) == 0 || strlen($this->_shortCode) == 0 || strlen($this->_keyWord) == 0) {
  107. throw new AfricasTalkingException('Please supply phoneNumber, shortCode and keyword');
  108. }
  109. $params = array(
  110. 'username' => $this->_username,
  111. 'phoneNumber' => $phoneNumber_,
  112. 'shortCode' => $this->_shortCode,
  113. 'keyword' => $this->_keyWord
  114. );
  115. $this->_requestUrl = self::SUBSCRIPTION_URL . "/delete";
  116. $this->_requestBody = http_build_query($params, '', '&');
  117. $this->executePOST();
  118. if ($this->_responseInfo['http_code'] != self::HTTP_CODE_CREATED)
  119. throw new AfricasTalkingException($this->_responseBody);
  120. return json_decode($this->_responseBody);
  121. }
  122. public function fetchPremiumSubscriptions($lastReceivedId_ = 0)
  123. {
  124. $username = $this->_username;
  125. $this->_requestUrl = self::SUBSCRIPTION_URL . '?username=' . $username . '&shortCode=' . $this->_shortCode;
  126. $this->_requestUrl .= '&keyword=' . $this->_keyWord . '&lastReceivedId=' . intval($lastReceivedId_);
  127. $this->executeGet();
  128. if ($this->_responseInfo['http_code'] == self::HTTP_CODE_OK) {
  129. $responseObject = json_decode($this->_responseBody);
  130. return $responseObject->responses;
  131. }
  132. throw new AfricasTalkingException($this->_responseBody);
  133. }
  134. //Call methods
  135. public function call($from_, $to_)
  136. {
  137. if (strlen($from_) == 0 || strlen($to_) == 0) {
  138. throw new AfricasTalkingException('Please supply both from and to parameters');
  139. }
  140. $params = array(
  141. 'username' => $this->_username,
  142. 'from' => $from_,
  143. 'to' => $to_
  144. );
  145. $this->_requestUrl = self::VOICE_URL . "/call";
  146. $this->_requestBody = http_build_query($params, '', '&');
  147. $this->executePOST();
  148. if (($responseObject = json_decode($this->_responseBody)) !== null) {
  149. if ($responseObject->Status != "Success")
  150. throw new AfricasTalkingException($responseObject->ErrorMessage);
  151. } else
  152. throw new AfricasTalkingException($this->_responseBody);
  153. }
  154. public function getNumQueuedCalls($phoneNumber_, $queueName = null)
  155. {
  156. $this->_requestUrl = self::VOICE_URL . "/queueStatus";
  157. $params = array(
  158. "username" => $this->_username,
  159. "phoneNumbers" => $phoneNumber_
  160. );
  161. if ($queueName !== null)
  162. $params['queueName'] = $queueName;
  163. $this->_requestBody = http_build_query($params, '', '&');
  164. $this->executePOST();
  165. if (($responseObject = json_decode($this->_responseBody)) !== null) {
  166. if ($responseObject->Status == "Success")
  167. return $responseObject->NumQueued;
  168. throw new AfricasTalkingException($responseObject->ErrorMessage);
  169. }
  170. throw new AfricasTalkingException($this->_responseBody);
  171. }
  172. public function uploadMediaFile($url_)
  173. {
  174. $params = array(
  175. "username" => $this->_username,
  176. "url" => $url_
  177. );
  178. $this->_requestBody = http_build_query($params, '', '&');
  179. $this->_requestUrl = self::VOICE_URL . "/mediaUpload";
  180. $this->executePOST();
  181. if (($responseObject = json_decode($this->_responseBody)) !== null) {
  182. if ($responseObject->Status != "Success")
  183. throw new AfricasTalkingException($responseObject->ErrorMessage);
  184. } else
  185. throw new AfricasTalkingException($this->_responseBody);
  186. }
  187. //Airtime method
  188. public function sendAirtime($recipients)
  189. {
  190. $params = array(
  191. "username" => $this->_username,
  192. "recipients" => $recipients
  193. );
  194. $this->_requestUrl = self::AIRTIME_URL . "/send";
  195. $this->_requestBody = http_build_query($params, '', '&');
  196. $this->executePOST();
  197. if ($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) {
  198. $responseObject = json_decode($this->_responseBody);
  199. if (count($responseObject->responses) > 0)
  200. return $responseObject->responses;
  201. throw new AfricasTalkingException($responseObject->errorMessage);
  202. }
  203. throw new AfricasTalkingException($this->_responseBody);
  204. }
  205. //User info method
  206. public function getUserData()
  207. {
  208. $username = $this->_username;
  209. $this->_requestUrl = self::USER_DATA_URL . '?username=' . $username;
  210. $this->executeGet();
  211. if ($this->_responseInfo['http_code'] == self::HTTP_CODE_OK) {
  212. $responseObject = json_decode($this->_responseBody);
  213. return $responseObject->UserData;
  214. }
  215. throw new AfricasTalkingException($this->_responseBody);
  216. }
  217. private function executeGet()
  218. {
  219. $ch = curl_init();
  220. $this->doExecute($ch);
  221. }
  222. private function executePost()
  223. {
  224. $ch = curl_init();
  225. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_requestBody);
  226. curl_setopt($ch, CURLOPT_POST, 1);
  227. $this->doExecute($ch);
  228. }
  229. private function doExecute(&$curlHandle_)
  230. {
  231. try {
  232. $this->setCurlOpts($curlHandle_);
  233. $responseBody = curl_exec($curlHandle_);
  234. if (self::Debug) {
  235. echo "Full response: " . print_r($responseBody, true) . "\n";
  236. }
  237. $this->_responseInfo = curl_getinfo($curlHandle_);
  238. $this->_responseBody = $responseBody;
  239. curl_close($curlHandle_);
  240. } catch (Exeption $e) {
  241. curl_close($curlHandle_);
  242. throw $e;
  243. }
  244. }
  245. private function setCurlOpts(&$curlHandle_)
  246. {
  247. curl_setopt($curlHandle_, CURLOPT_TIMEOUT, 60);
  248. curl_setopt($curlHandle_, CURLOPT_SSL_VERIFYPEER, FALSE);
  249. curl_setopt($curlHandle_, CURLOPT_URL, $this->_requestUrl);
  250. curl_setopt($curlHandle_, CURLOPT_RETURNTRANSFER, true);
  251. curl_setopt($curlHandle_, CURLOPT_HTTPHEADER, array('Accept: application/json',
  252. 'apikey: ' . $this->_apiKey));
  253. }
  254. }
  255. class AfricasTalkingException extends \Exception{}