SmsApiClient.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. require_once 'ApiClient.php';
  3. class SmsApiClient extends ApiClient {
  4. private $sender;
  5. private $tariff;
  6. public function __construct($api_key, $sender, $tariff=null, $source = null)
  7. {
  8. $this->sender = $sender;
  9. $this->tariff = $tariff;
  10. parent::__construct($api_key, $source);
  11. }
  12. private function getStatus ($endpoint)
  13. {
  14. try {
  15. $resp = $this->sendRequest($endpoint);
  16. } catch (Exception $e) {
  17. $error = 'Request failed (code: ' .$e->getCode() .'): ' . $e->getMessage();
  18. return array('error' => $error);
  19. }
  20. $result = json_decode($resp,true);
  21. return $result;
  22. }
  23. public function getStatusByReference ($reference)
  24. {
  25. return $this->getStatus ('sms/reference/' . $reference);
  26. }
  27. public function getStatusById ($message_id)
  28. {
  29. return $this->getStatus ('sms/' . $message_id);
  30. }
  31. public function getTaskStatus ($task_id)
  32. {
  33. try {
  34. $resp = $this->sendRequest('sms/task/' . $task_id);
  35. } catch (Exception $e) {
  36. $error = 'Request failed (code: ' .$e->getCode() .'): ' . $e->getMessage();
  37. return array('error' => $error);
  38. }
  39. $result = json_decode($resp,true);
  40. return $result;
  41. }
  42. public function getPrices($tariff=NULL)
  43. {
  44. try {
  45. $resp = $this->sendRequest('sms/prices' . ($tariff !== NULL ? ('/' . $tariff) : ''));
  46. } catch (Exception $e) {
  47. $error = 'Request failed (code: ' .$e->getCode() .'): ' . $e->getMessage();
  48. return array('error' => $error);
  49. }
  50. $result = json_decode($resp,true);
  51. return $result;
  52. }
  53. public function getPrice ($msisdn, $originator, $body, $reference, $validity=72, $tariff=NULL)
  54. {
  55. $originator = $originator ?: $this->sender;
  56. $tariff = $tariff ?: $this->tariff;
  57. return $this->sendSms ($msisdn, $body, $reference, $validity, $tariff, $originator, true);
  58. }
  59. public function sendSms ($msisdn, $body, $reference, $validity=72, $tariff=NULL, $originator = null, $only_price=false)
  60. {
  61. $originator = $originator ?: $this->sender;
  62. $tariff = $tariff ?: $this->tariff;
  63. $message = array();
  64. $message['destination'] = 'phone';
  65. $message['msisdn'] = $msisdn;
  66. $message['originator'] = $originator;
  67. $message['body'] = $body;
  68. $message['reference'] = $reference;
  69. $message['validity'] = $validity;
  70. if ($tariff !== NULL)
  71. $message['tariff'] = $tariff;
  72. $endpoint = $only_price ? 'sms/price' : 'sms/create';
  73. try {
  74. $resp = $this->sendRequest($endpoint,json_encode($message),'PUT');
  75. } catch (Exception $e) {
  76. $error = 'Request failed (code: ' .$e->getCode() .'): ' . $e->getMessage();
  77. return array('error' => $error);
  78. }
  79. $result = json_decode($resp,true);
  80. return $result;
  81. }
  82. public function getTaskPrice ($msisdns, $body, $validity=72, $tariff=NULL, $originator=NULL)
  83. {
  84. return $this->sendTask ($msisdns, $body, $validity, $tariff, $originator, true);
  85. }
  86. /**
  87. * sends the sms text to array of destination numbers
  88. * $msisdns are array of [$msisdn, $reference]
  89. * @param $msisdns
  90. * @param $originator
  91. * @param $body
  92. * @param int $validity
  93. * @param null $tariff
  94. * @param $only_price
  95. *
  96. * @return array
  97. */
  98. public function sendTask ($msisdns, $body, $validity=72, $tariff=NULL, $originator=NULL, $only_price=false)
  99. {
  100. $originator = $originator ?: $this->sender;
  101. $message = array();
  102. $message['destination'] = 'phones';
  103. $message['phones'] = $msisdns;
  104. $message['originator'] = $originator;
  105. $message['body'] = $body;
  106. $message['validity'] = $validity;
  107. if ($tariff !== NULL)
  108. $message['tariff'] = $tariff;
  109. $endpoint = $only_price ? 'sms/price' : 'sms/create';
  110. try {
  111. $resp = $this->sendRequest($endpoint,json_encode($message),'PUT');
  112. } catch (Exception $e) {
  113. $error = 'Request failed (code: ' .$e->getCode() .'): ' . $e->getMessage();
  114. return array('error' => $error);
  115. }
  116. $result = json_decode($resp,true);
  117. return $result;
  118. }
  119. public function getMultiPrice ($messages, $validity=72, $tariff=NULL)
  120. {
  121. return $this->sendSmsMulti ($messages, $validity, $tariff, true);
  122. }
  123. /**
  124. * sends a few sms with different senders / originators
  125. * @param $messages
  126. * @param int $validity
  127. * @param null $tariff
  128. * @param $only_price
  129. *
  130. * @return array
  131. */
  132. public function sendSmsMulti ($messages, $validity=72, $tariff=NULL, $only_price=false)
  133. {
  134. foreach ($messages as &$msg)
  135. if(!isset($msg['originator']) && $this->sender)
  136. $msg['originator'] = $this->sender;
  137. $message = array();
  138. $message['destination'] = 'individual';
  139. $message['phones'] = $messages;
  140. $message['validity'] = $validity;
  141. if ($tariff !== NULL)
  142. $message['tariff'] = $tariff;
  143. $endpoint = $only_price ? 'sms/price' : 'sms/create';
  144. try {
  145. $resp = $this->sendRequest($endpoint,json_encode($message),'PUT');
  146. } catch (Exception $e) {
  147. $error = 'Request failed (code: ' .$e->getCode() .'): ' . $e->getMessage();
  148. return array('error' => $error);
  149. }
  150. $result = json_decode($resp,true);
  151. return $result;
  152. }
  153. }