<?php
trait Http
{
protected function request($url, $data = [], $method = 'get', $options = [], $returnArray = true)
{
$curl = curl_init();
is_array($data) && $data = http_build_query($data);
$method = strtoupper($method);
if ($method == 'GET') {
$url .= stripos($url, '?') !== false ? '&' : '?';
$url .= $data;
} else {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
unset($options[CURLOPT_URL], $options[CURLOPT_POST], $options[CURLOPT_POSTFIELDS]);
$_options = [
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_AUTOREFERER => 1,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
CURLOPT_HEADER => 0,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => 1,
];
$options = ($_options + $options);
curl_setopt_array($curl, $options);
$return = curl_exec($curl);
curl_close($curl);
if ($returnArray) {
$return = json_decode($return, true);
}
return $return;
}
protected function getRequest($url, $data =[], $options = [], $returnArray = true)
{
return $this->request($url, $data, 'get', $options, $returnArray);
}
protected function postRequest($url, $data = [], $options = [], $returnArray = true)
{
return $this->request($url, $data, 'post', $options, $returnArray);
}
}