forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.4 KiB
144 lines
3.4 KiB
<?php |
|
|
|
namespace app\Providers; |
|
|
|
class Requests |
|
{ |
|
/** @var string $token token de autenticação das requesições */ |
|
protected $token; |
|
|
|
/** @var string $url url base das requsições */ |
|
protected $url; |
|
|
|
/** @var string $metodo endpoint da requsição */ |
|
protected $metodo; |
|
|
|
/** @var string $query query da requsição */ |
|
protected $query; |
|
|
|
/** @var string $query tipo da requsição */ |
|
protected $requestType; |
|
|
|
/** @var array $params parametros da request */ |
|
protected $params = array(); |
|
|
|
/** @var RequestURL $request description */ |
|
protected $request; |
|
|
|
/** @var string $contentType tipo da requsiçã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��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; |
|
} |
|
} |