HLRApiClient.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. require_once 'ApiClient.php';
  3. class HLRApiClient extends ApiClient {
  4. protected $tariff;
  5. public function __construct($api_key, $tariff = null, $source = null) {
  6. $this->tariff = $tariff;
  7. parent::__construct($api_key, $source);
  8. }
  9. private function getStatus($endpoint) {
  10. try {
  11. $resp = $this->sendRequest($endpoint);
  12. } catch (Exception $e) {
  13. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  14. return array('error' => $error);
  15. }
  16. return json_decode($resp, true);
  17. }
  18. public function getStatusByReference($reference) {
  19. return $this->getStatus('hlr/reference/' . $reference);
  20. }
  21. public function getStatusById($message_id) {
  22. return $this->getStatus('hlr/' . $message_id);
  23. }
  24. public function getPrices($tariff = NULL) {
  25. try {
  26. $resp = $this->sendRequest('hlr/prices' . ($tariff !== NULL ? ('/' . $tariff) : ''));
  27. } catch (Exception $e) {
  28. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  29. return array('error' => $error);
  30. }
  31. $result = json_decode($resp, true);
  32. return $result;
  33. }
  34. public function sendHLR($msisdn, $reference, $tariff = NULL) {
  35. $tariff = $tariff ?: $this->tariff;
  36. $message = arrray();
  37. $message['destination'] = 'phone';
  38. $message['msisdn'] = $msisdn;
  39. $message['reference'] = $reference;
  40. if ($tariff !== NULL)
  41. $message['tariff'] = $tariff;
  42. try {
  43. $resp = $this->sendRequest('hlr/create', $message);
  44. } catch (Exception $e) {
  45. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  46. return array('error' => $error);
  47. }
  48. $result = json_decode($resp, true);
  49. return $result;
  50. }
  51. /**
  52. * sends multiply HLR requests. $payload must contain array of arrays:
  53. * [$msisdn, $reference, $tariff, $callback_url], where $tariff and $callback_url
  54. * are optional
  55. * @param $payload
  56. * @return array
  57. */
  58. public function sendHLRs($payload) {
  59. try {
  60. $resp = $this->sendRequest('hlr/create', json_encode($payload), 'PUT');
  61. } catch (Exception $e) {
  62. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  63. return array('error' => $error);
  64. }
  65. $result = json_decode($resp, true);
  66. return $result;
  67. }
  68. }