/application/controllers/Admin/lib/centaurosms.php

https://gitlab.com/adrianjose605/SaintW · PHP · 123 lines · 90 code · 14 blank · 19 comment · 11 complexity · 71fe4493b3dfe49a097f77d0570360a3 MD5 · raw file

  1. <?php
  2. /**
  3. * Libreria de Integracion CentauroSMS v1.2
  4. * API para Integracion de Envios de SMS a cualquier Aplicacion Web
  5. *
  6. * @autor Hernan Crespo
  7. *
  8. */
  9. class CentauroSMS {
  10. private $client_id;
  11. private $client_secret;
  12. private $result_data;
  13. function __construct($client_id, $client_secret) {
  14. $this->client_id = $client_id;
  15. $this->client_secret = $client_secret;
  16. }
  17. /**
  18. * Obtener numero de SMS disponibles
  19. */
  20. public function get_sms_disponibles() {
  21. $credenciales = $this->cParametros(array(
  22. 'client_id' => $this->client_id,
  23. 'client_secret' => $this->client_secret,
  24. 'client_opcion' => 'sms_disponibles'));
  25. return $result_data = cSMSClient::post("/controllersms/", $credenciales, "application/x-www-form-urlencoded");
  26. }
  27. /**
  28. * Envio de SMS Masivos normales
  29. */
  30. public function set_sms_send($json,$msg) {
  31. $credenciales = $this->cParametros(array(
  32. 'client_id' => $this->client_id,
  33. 'client_secret' => $this->client_secret,
  34. 'json' => base64_encode(urlencode($json)),
  35. 'msg' => base64_encode(urlencode($msg)),
  36. 'client_opcion' => 'send_sms'));
  37. return $result_data = cSMSClient::post("/controllersms/", $credenciales, "application/x-www-form-urlencoded");
  38. }
  39. /**
  40. * Envio de SMS Masivos personalizados
  41. */
  42. public function set_sms_send_personalizado($json){
  43. $credenciales = $this->cParametros(array(
  44. 'client_id' => $this->client_id,
  45. 'client_secret' => $this->client_secret,
  46. 'json' => base64_encode(urlencode($json)),
  47. 'client_opcion' => 'send_sms_personalizado'));
  48. return $result_data = cSMSClient::post("/controllersms/", $credenciales, "application/x-www-form-urlencoded");
  49. }
  50. private function cParametros($params) {
  51. if (function_exists("http_build_query")) {
  52. return http_build_query($params, "", "&");
  53. } else {
  54. foreach ($params as $name => $value) {
  55. $elements[] = "{$name}=" . urlencode($value);
  56. }
  57. return implode("&", $elements);
  58. }
  59. }
  60. }
  61. /**
  62. * CentauroSMS cURL Cliente
  63. */
  64. class cSMSClient{
  65. const API_BASE_URL = "http://api.centaurosms.com.ve";
  66. private static function get_connect($uri, $method, $content_type) {
  67. $connect = curl_init(self::API_BASE_URL . $uri);
  68. curl_setopt($connect, CURLOPT_RETURNTRANSFER, true);
  69. curl_setopt($connect, CURLOPT_CUSTOMREQUEST, $method);
  70. curl_setopt($connect, CURLOPT_HTTPHEADER, array("Accept: application/json", "Content-Type: " . $content_type));
  71. return $connect;
  72. }
  73. private static function set_data(&$connect, $data, $content_type) {
  74. if ($content_type == "application/json") {
  75. if (gettype($data) == "string") {
  76. json_decode($data, true);
  77. } else {
  78. $data = json_encode($data);
  79. }
  80. if(function_exists('json_last_error')) {
  81. $json_error = json_last_error();
  82. if ($json_error != JSON_ERROR_NONE) {
  83. throw new Exception("JSON Error [{$json_error}] - Data: {$data}");
  84. }
  85. }
  86. }
  87. curl_setopt($connect, CURLOPT_POSTFIELDS, $data);
  88. }
  89. private static function exec($method, $uri, $data, $content_type) {
  90. $connect = self::get_connect($uri, $method, $content_type);
  91. if ($data) {
  92. self::set_data($connect, $data, $content_type);
  93. }
  94. $api_result = curl_exec($connect);
  95. $response = json_decode($api_result, true);
  96. curl_close($connect);
  97. return $response;
  98. }
  99. public static function get($uri, $content_type = "application/json") {
  100. return self::exec("GET", $uri, null, $content_type);
  101. }
  102. public static function post($uri, $data, $content_type = "application/json") {
  103. return self::exec("POST", $uri, $data, $content_type);
  104. }
  105. public static function put($uri, $data, $content_type = "application/json") {
  106. return self::exec("PUT", $uri, $data, $content_type);
  107. }
  108. }
  109. ?>