forked from Claudio/integracao
douglasstrappasson
1 year ago
8 changed files with 209 additions and 17 deletions
@ -0,0 +1,23 @@
|
||||
<?php |
||||
|
||||
require_once "Integracao.php"; |
||||
include 'config.php'; |
||||
|
||||
/** |
||||
* Classe para utilizar as API da empresa 21Mobile |
||||
* |
||||
* @documentation: https://api.21mobile.com.br/docs/v1/http |
||||
* @author Douglas Fernando Strappasson |
||||
* @function junior developer |
||||
* @company SimplesIP |
||||
* @version 1.0.1 |
||||
*/ |
||||
|
||||
class Mobile21 extends Integracao { |
||||
|
||||
$token = base64_encode("USERNAME" . ":" . "PASSWORD"); |
||||
$messageText = "TESTE API v1 21MOBILE"; |
||||
$destination = "11999999999"; |
||||
$correlationId = "idDeControle"; |
||||
|
||||
}; |
@ -0,0 +1,47 @@
|
||||
<?php |
||||
|
||||
$curl = curl_init(); |
||||
|
||||
$token = base64_encode("USERNAME" . ":" . "PASSWORD"); |
||||
$messageText = "TESTE API v1 21MOBILE"; |
||||
$destination = "11999999999"; |
||||
$correlationId = "idDeControle"; |
||||
|
||||
$json = json_encode( |
||||
array( |
||||
"sms" => array( |
||||
array( |
||||
"messageText" => $messageText, |
||||
"destination" => $destination, |
||||
"correlationId" => $correlationId |
||||
) |
||||
) |
||||
) |
||||
); |
||||
|
||||
curl_setopt_array($curl, array( |
||||
CURLOPT_URL => "https://api.21mobile.com.br/v1/send", |
||||
CURLOPT_RETURNTRANSFER => true, |
||||
CURLOPT_ENCODING => "", |
||||
CURLOPT_MAXREDIRS => 10, |
||||
CURLOPT_TIMEOUT => 30, |
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||
CURLOPT_CUSTOMREQUEST => "POST", |
||||
CURLOPT_POSTFIELDS => $json, |
||||
CURLOPT_HTTPHEADER => array( |
||||
"authorization: token$token $token", |
||||
"cache-control: no-cache", |
||||
"content-type: application/json", |
||||
), |
||||
)); |
||||
|
||||
$response = curl_exec($curl); |
||||
$err = curl_error($curl); |
||||
|
||||
curl_close($curl); |
||||
|
||||
if ($err) { |
||||
echo "cURL Error #:" . $err; |
||||
} else { |
||||
echo $response; |
||||
} |
@ -0,0 +1,69 @@
|
||||
<?php |
||||
function enviaSMSMobile($numeroDestino, $texto, $clientId = ""){ |
||||
$user = 'simples_ip'; |
||||
$senha = '2imms0f'; |
||||
$token = base64_encode($user.":".$senha); |
||||
$minDate = 1672545600; |
||||
$metodo = 'send'; |
||||
$request = 'POST'; |
||||
$params = array( |
||||
"sms" => array( |
||||
array( |
||||
"messageText" => $texto, |
||||
"destination" => $numeroDestino |
||||
) |
||||
) |
||||
); |
||||
|
||||
return setParams($request, $params, $metodo, $token); |
||||
|
||||
|
||||
} |
||||
|
||||
function setParams($request, $params, $metodo, $token){ |
||||
if (strtoupper($request) == "POST"){ |
||||
$query = json_encode($params); |
||||
} |
||||
|
||||
if (strtoupper($request) == "GET"){ |
||||
if($params){ |
||||
$count = 0; |
||||
$query = "?"; |
||||
foreach ($params as $key => $value){ |
||||
$query .= "{$key}={$value}"; |
||||
if ($params != ++$count){ |
||||
$query .= "&"; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
unset($params); |
||||
|
||||
$curl = curl_init(); |
||||
curl_setopt_array($curl, array( |
||||
CURLOPT_URL => "https://api.21mobile.com.br/v1/{$metodo}", |
||||
CURLOPT_RETURNTRANSFER => true, |
||||
CURLOPT_ENCODING => "", |
||||
CURLOPT_MAXREDIRS => 10, |
||||
CURLOPT_TIMEOUT => 30, |
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||
CURLOPT_POSTFIELDS => strtoupper($request) == "POST" ? "POST" : "", |
||||
CURLOPT_CUSTOMREQUEST => strtoupper($request), |
||||
CURLOPT_HTTPHEADER => array( |
||||
"authorization: Basic $token", |
||||
"cache-control: no-cache", |
||||
"content-type: application/json", |
||||
), |
||||
)); |
||||
|
||||
$response = curl_exec($curl); |
||||
$err = curl_error($curl); |
||||
|
||||
curl_close($curl); |
||||
|
||||
if ($err) { |
||||
throw new Exception("ERRO! - . $err"); |
||||
} else { |
||||
return $response; |
||||
} |
||||
} |
@ -0,0 +1 @@
|
||||
Requisicao invalida para o WebHook! A requisicao foi delegada. |
@ -0,0 +1,60 @@
|
||||
<?php |
||||
|
||||
####################################### |
||||
### WEBHOOK PARA 21Moblie SMS ### |
||||
####################################### |
||||
|
||||
/** |
||||
* Este script tem a finalidade de receber as requisicoes via GET do webhook |
||||
* e atualizar o status do sms no banco de dados. |
||||
*/ |
||||
|
||||
include ("bd.php"); |
||||
|
||||
define('CONF_LOGGER_REQUEST', '/var/log/asterisk/sms_mobile.log'); |
||||
|
||||
setLog("Edereco IP: ".$_SERVER["REMOTE_ADDR"]); |
||||
|
||||
// Desativa a exibição de erros na tela do navegador em seguida direciona os erros para o arquivo de log |
||||
ini_set('display_errors', 'Off'); |
||||
ini_set('error_log', CONF_LOGGER_REQUEST); |
||||
|
||||
header("Access-Control-Allow-Origin: *"); |
||||
header("Content-Type: application/json; charset=UTF-8"); |
||||
header("Access-Control-Allow-Methods: GET"); |
||||
header("Access-Control-Max-Age: 3600"); |
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, x-amzn-trace-id"); |
||||
|
||||
try { |
||||
$data = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS); |
||||
$errorParam = "ERRO: Requisicao invalida para o WebHook! Os parametros nao foram informados."; |
||||
|
||||
extract($data); |
||||
if (!isset($correlationId) || !isset($status)){ |
||||
throw new Exception($errorParam); |
||||
} |
||||
|
||||
updateData($dbcon, $correlationId, $status); |
||||
return http_response_code(200); |
||||
|
||||
} catch (Exception $ex) { |
||||
setLog($ex->getMessage()); |
||||
return http_response_code(400); |
||||
} |
||||
|
||||
function setLog($msgError) { |
||||
file_put_contents(CONF_LOGGER_REQUEST, $msgError . date(' d-m-Y - H:i:s') . PHP_EOL, FILE_APPEND); |
||||
} |
||||
|
||||
/** |
||||
* esta funcao realiza o update no banco de dados. |
||||
*/ |
||||
function updateData($dbcon, $correlationId, $status) { |
||||
$errorDb = "ERRO: Nao foi possivel realizar a insercao no banco de dados!"; |
||||
$query = sprintf("UPDATE pbx_sms_send SET sms_status = '%s' WHERE sms_id_envio = '%s'", $status, $correlationId); |
||||
pg_query($dbcon, $query); |
||||
|
||||
if (pg_last_error()) { |
||||
throw new Exception($errorDb . pg_last_error()); |
||||
} |
||||
} |
Loading…
Reference in new issue