|
|
<?php |
|
|
|
|
|
require_once 'Integracao.php'; |
|
|
include "config.php"; |
|
|
|
|
|
/** |
|
|
* DESCRICAO DO DESENVOLVEDOR |
|
|
* |
|
|
* @author Lucas Awade |
|
|
* @function developer |
|
|
* @company SimplesIP |
|
|
* @version 1.0.0 |
|
|
*/ |
|
|
class Trackersale extends Integracao { |
|
|
|
|
|
private $token; |
|
|
private $url; |
|
|
private $metodo; |
|
|
private $query; |
|
|
private $curl; |
|
|
private $debug; |
|
|
private $params = array(); |
|
|
|
|
|
######################################################################## |
|
|
## FUNCOES DA API ## |
|
|
######################################################################## |
|
|
|
|
|
public function answer($code, $client, $score, $name, $value, $email, $justication = 'PESQUISA URA') { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->params = array( |
|
|
"campaign_code" => $code, |
|
|
"answers" => array( |
|
|
array( |
|
|
"name" => $client, |
|
|
"email" => $email, |
|
|
"score" => $score, |
|
|
"justification" => $justication, |
|
|
"tags" => array( |
|
|
array( |
|
|
"name" => $name, |
|
|
"value" => $value |
|
|
) |
|
|
) |
|
|
) |
|
|
) |
|
|
); |
|
|
|
|
|
$this->request('POST'); |
|
|
$this->setMetodo('answer'); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
public function campaign() { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->setMetodo('campaign'); |
|
|
$this->request('GET'); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
######################################################################## |
|
|
## FUNCOES DEFAULT DA CLASSE ## |
|
|
######################################################################## |
|
|
|
|
|
/** |
|
|
* Coleta as informacoes iniciais para o inicio da integracao com a API. |
|
|
* |
|
|
* @param string $token |
|
|
* @param string $url |
|
|
* @param boolean $log |
|
|
*/ |
|
|
public function __construct() { |
|
|
$this->token = CONF_TOKEN_API; |
|
|
$this->url = CONF_URL_API; |
|
|
$this->setLog(CONF_LOGGER_ATIVO); |
|
|
$this->log->info("Iniciando integracao", debug_backtrace()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Parametriza o metodo utilizado para a consulta. |
|
|
* |
|
|
* @param type $metodo |
|
|
*/ |
|
|
private function setMetodo($metodo) { |
|
|
$this->metodo = $metodo; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Escreve a query para ser passada para o curl |
|
|
* |
|
|
* @param string $query |
|
|
*/ |
|
|
private function setQuery($query) { |
|
|
return $this->query .= $query; |
|
|
} |
|
|
|
|
|
/** |
|
|
* retorna a string pronta da query do curl e limpa a variavel. |
|
|
* |
|
|
* @return string $query |
|
|
*/ |
|
|
private function getQuery() { |
|
|
$query = $this->query; |
|
|
unset($this->query); |
|
|
return $query; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Informa o tipo de requisicao que sera feita pela cURL |
|
|
* @param bool $request |
|
|
*/ |
|
|
private function request($request = null) { |
|
|
if (!$this->request || $request) { |
|
|
$this->request = (strtoupper($request) == 'POST' ? strtoupper($request) : "GET"); |
|
|
} |
|
|
$this->log->debug("HTTP Request: " . $this->request, debug_backtrace()); |
|
|
return $this->request; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Constroi de forma dinamica a string para ser passada para a execucao do Curl |
|
|
* |
|
|
* @void |
|
|
*/ |
|
|
private function curl() { |
|
|
/* |
|
|
* Final da linha para pegar os dados da variavel |
|
|
*/ |
|
|
$this->curl = curl_init(); |
|
|
curl_setopt($this->curl, CURLOPT_URL, $this->url . $this->metodo); |
|
|
$header = array('Content-Type: application/json', "Authorization: Bearer {$this->token}"); |
|
|
|
|
|
if ($this->request == 'POST') { |
|
|
curl_setopt($this->curl, CURLOPT_POST, true); |
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->getQuery()); |
|
|
} |
|
|
|
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); |
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
|
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10); |
|
|
/* |
|
|
* Final da linha para pegar os dados da variavel |
|
|
*/ |
|
|
$this->log->debug("Curl: {$this->curl}", debug_backtrace()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Recebe array de forma de indice e valor |
|
|
* |
|
|
* @example array("qtype" => 'test_api', "query" => '123', "oper" => '=') |
|
|
* |
|
|
* @obs sempre chamar a fun<EFBFBD><EFBFBD>o debug_backtrace() para ser dispon<EFBFBD>vel em log |
|
|
* |
|
|
* @param type $params |
|
|
* @debug_track function debug_backtrace() |
|
|
*/ |
|
|
private function setParams() { |
|
|
$json = json_encode($this->params, true); |
|
|
$this->setQuery($json); |
|
|
$this->curl(); |
|
|
unset($this->params); |
|
|
return $this->exec(); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Recebe as informa<EFBFBD><EFBFBD>es e realiza a execucao da API |
|
|
* |
|
|
* @void |
|
|
*/ |
|
|
private function exec() { |
|
|
/** |
|
|
* Caso tenha problema de requisi<EFBFBD><EFBFBD>o |
|
|
*/ |
|
|
$content = curl_exec($this->curl); |
|
|
if (curl_errno($this->curl)) { |
|
|
$this->log->error(curl_error($this->curl), debug_backtrace()); |
|
|
curl_close($this->curl); |
|
|
//$this->audioError(); |
|
|
return false; |
|
|
} |
|
|
curl_close($this->curl); |
|
|
return $this->response($content); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Prepara dos dados para ser transmitidos para o metodo a ser retornado a |
|
|
* integracao. |
|
|
* |
|
|
* @return array |
|
|
*/ |
|
|
private function response($data) { |
|
|
$this->log->debug("Reponse API: " . print_r($data, true), $this->debug); |
|
|
if ($data) { |
|
|
return json_decode($data); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|