|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\Providers;
|
|
|
|
|
|
|
|
|
|
class Requests
|
|
|
|
|
{
|
|
|
|
|
/** @var string $token token de autentica<EFBFBD><EFBFBD>o das requesi<EFBFBD><EFBFBD>es */
|
|
|
|
|
protected $token;
|
|
|
|
|
|
|
|
|
|
/** @var string $url url base das requsi<EFBFBD><EFBFBD>es */
|
|
|
|
|
protected $url;
|
|
|
|
|
|
|
|
|
|
/** @var string $metodo endpoint da requsi<EFBFBD><EFBFBD>o */
|
|
|
|
|
protected $metodo;
|
|
|
|
|
|
|
|
|
|
/** @var string $query query da requsi<EFBFBD><EFBFBD>o */
|
|
|
|
|
protected $query;
|
|
|
|
|
|
|
|
|
|
/** @var string $query tipo da requsi<EFBFBD><EFBFBD>o */
|
|
|
|
|
protected $requestType;
|
|
|
|
|
|
|
|
|
|
/** @var array $params parametros da request */
|
|
|
|
|
protected $params = array();
|
|
|
|
|
|
|
|
|
|
/** @var RequestURL $request description */
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
|
|
/** @var string $contentType tipo da requsi<EFBFBD><EFBFBD>o */
|
|
|
|
|
protected $contentType;
|
|
|
|
|
|
|
|
|
|
/** @var string $storage caminho dos arquivos */
|
|
|
|
|
public $storage = CONF_PATH_FILES;
|
|
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->request = new RequestURL();
|
|
|
|
|
$this->setToken();
|
|
|
|
|
$this->setUrl(CONF_WHATSAPP_AUTH_URL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function response($result)
|
|
|
|
|
{
|
|
|
|
|
if ($result) {
|
|
|
|
|
if (json_decode($result, true) !== null) {
|
|
|
|
|
return json_decode($result, true);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function exec($typeAuth = 'Bearer')
|
|
|
|
|
{
|
|
|
|
|
$this->setQuery(json_encode($this->params)); //SET QUERY
|
|
|
|
|
$this->request->setUrl($this->url . $this->metodo, false);
|
|
|
|
|
|
|
|
|
|
$header = array();
|
|
|
|
|
|
|
|
|
|
$header[] = "Authorization: $typeAuth {$this->token}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->requestType == 'POST') {
|
|
|
|
|
$header[] = 'Content-Type: application/json';
|
|
|
|
|
$this->request->post_field($this->getQuery(), true);
|
|
|
|
|
}
|
|
|
|
|
$this->request->header($header);
|
|
|
|
|
$this->request->method_request($this->requestType);
|
|
|
|
|
logger('request')->info(print_r($header, true));
|
|
|
|
|
logger('request')->info($this->requestType);
|
|
|
|
|
return $this->request->exec_request();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recebe o tipo de Requisi<EFBFBD><EFBFBD>o GET/POST
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
function requestType($req = null)
|
|
|
|
|
{
|
|
|
|
|
if (!$req) {
|
|
|
|
|
return $this->requestType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strtoupper($req) == "GET") {
|
|
|
|
|
return $this->requestType = "GET";
|
|
|
|
|
} else if (strtoupper($req) == "POST") {
|
|
|
|
|
return $this->requestType = "POST";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verifica se todos os parametros passados foram completados.
|
|
|
|
|
*
|
|
|
|
|
* @param array $args
|
|
|
|
|
* @return true|false
|
|
|
|
|
*/
|
|
|
|
|
function getArgs($args)
|
|
|
|
|
{
|
|
|
|
|
foreach ($args as $value) {
|
|
|
|
|
if (!$value) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setMetodo($metodo)
|
|
|
|
|
{
|
|
|
|
|
$this->metodo = $metodo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Escreve a query para ser passada para o curl
|
|
|
|
|
*
|
|
|
|
|
* @param string $query
|
|
|
|
|
*/
|
|
|
|
|
function setQuery($query)
|
|
|
|
|
{
|
|
|
|
|
return $this->query .= $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* retorna a string pronta da query do curl e limpa a variavel.
|
|
|
|
|
*
|
|
|
|
|
* @return string $query
|
|
|
|
|
*/
|
|
|
|
|
function getQuery()
|
|
|
|
|
{
|
|
|
|
|
$query = $this->query;
|
|
|
|
|
unset($this->query);
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setToken($token = CONF_WHATSAPP_AUTH_TOKEN)
|
|
|
|
|
{
|
|
|
|
|
$this->token = $token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setUrl($url)
|
|
|
|
|
{
|
|
|
|
|
$this->url = $url;
|
|
|
|
|
}
|
|
|
|
|
}
|