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.
103 lines
2.9 KiB
103 lines
2.9 KiB
2 years ago
|
<?php
|
||
|
|
||
|
namespace app\Providers;
|
||
|
|
||
|
class RequestURL
|
||
|
{
|
||
|
|
||
|
private $method;
|
||
|
private $curl;
|
||
|
private $ssl;
|
||
|
private $post_field;
|
||
|
private $header;
|
||
|
|
||
|
const CONF_TIMEOUT = 30;
|
||
|
|
||
|
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);
|
||
|
curl_close($this->curl);
|
||
|
return $this->response($this->response);
|
||
|
}
|
||
|
|
||
|
public function convert_xml_to_object()
|
||
|
{
|
||
|
$this->debug = debug_backtrace();
|
||
|
if (!$this->response) {
|
||
|
return false;
|
||
|
}
|
||
|
return simplexml_load_string($this->response);
|
||
|
}
|
||
|
|
||
|
private function response($result)
|
||
|
{
|
||
|
// logger('deburguer')->info(print_r($result, true));
|
||
|
if ($result) {
|
||
|
if (json_decode($result, true) !== null) {
|
||
|
return json_decode($result, true);
|
||
|
}
|
||
|
return $result;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
############################################################################
|
||
|
### CONFIG. CURL ###
|
||
|
############################################################################
|
||
|
|
||
|
private function conf_request()
|
||
|
{
|
||
|
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
|
||
|
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);
|
||
|
}
|
||
|
}
|