forked from Claudio/integracao
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.
92 lines
3.1 KiB
92 lines
3.1 KiB
<?php |
|
|
|
require_once 'Logger.php'; |
|
require_once 'Integracao.php'; |
|
require_once 'config.php'; |
|
|
|
class RequestURL extends Integracao{ |
|
|
|
private $method; |
|
private $curl; |
|
private $ssl; |
|
private $post_field; |
|
private $header; |
|
|
|
const CONF_TIMEOUT = 30; |
|
|
|
function __construct($log = CONF_LOGGER_ATIVO) { |
|
$this->log = new Logger(CONF_LOGGER_PATH, $log); |
|
$this->integracaoReg(); |
|
} |
|
|
|
public function setUrl($url, $ssl = true){ |
|
$this->debug = debug_backtrace(); |
|
$this->curl = curl_init($url); |
|
$this->ssl = is_bool($ssl) ? $ssl : true; |
|
} |
|
|
|
public function header($header) { |
|
$this->debug = debug_backtrace(); |
|
if (is_array($header)) { |
|
$this->header = $header; |
|
} else { |
|
$this->header = array($header); |
|
} |
|
} |
|
|
|
public function post_field($data, $json = false) { |
|
$this->debug = debug_backtrace(); |
|
if ($json) { |
|
$this->post_field = $data; |
|
} else { |
|
$this->post_field = http_build_query($data); |
|
} |
|
} |
|
|
|
public function method_request($method = 'GET') { |
|
$this->debug = debug_backtrace(); |
|
$this->method = strtoupper($method); |
|
} |
|
|
|
public function exec_request() { |
|
$this->debug = debug_backtrace(); |
|
$this->conf_request(); |
|
|
|
if (curl_error($this->curl)) { |
|
return false; |
|
} |
|
$this->response = curl_exec($this->curl); |
|
$this->log->debug("Reponse API: " . print_r(json_decode($this->response),true), $this->debug); |
|
curl_close($this->curl); |
|
return $this->response; |
|
} |
|
|
|
public function convert_xml_to_object() { |
|
$this->debug = debug_backtrace(); |
|
if (!$this->response) { |
|
return false; |
|
} |
|
return simplexml_load_string($this->response); |
|
} |
|
|
|
############################################################################ |
|
### CONFIG. CURL ### |
|
############################################################################ |
|
|
|
private function conf_request() { |
|
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header); |
|
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $this->method); |
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, $this->ssl); |
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, $this->ssl); |
|
|
|
if ($this->post_field) { |
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->post_field); |
|
} |
|
|
|
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, self::CONF_TIMEOUT); |
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, self::CONF_TIMEOUT); |
|
$this->log->debug("Curl: ".print_r(curl_getinfo($this->curl), true), debug_backtrace()); |
|
} |
|
}
|
|
|