<?
class connect
{
//заголовки для подключения
private $_headers = [
'Authorization: Bearer',
'Accept: application/json',
'Content-type: application/json'
];
public function get($token, $url, array $data=null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url.($data!=null ? '?'.$this->prepare_get_params($data) : ''));
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers($token));
$out = curl_exec($curl);
curl_close($curl);
return $out;
}
private function prepare_get_params(array $data)
{
return http_build_query($data);
}
private function headers($token)
{
$headers = $this->_headers;
$headers[0] = $headers[0].' '.$token;
return $headers;
}
}
<?
require('connect.php');
class kd
{
const URL = [
'search' => 'https://site.ru/v2/search',
];
public static function search($token, array $data)
{
return self::connect()->get($token,self::URL['search'],$data);
}
private static function connect()
{
return new connect();
}
}
$token = '1ktiiCOMQsvlPjJm49DiO';
$data = array(7004138, 7004254, 7017639);
$obj = new kd;
$json=$obj->search($token, $data);
$data = json_decode($json, true);
print_r($data);
class connect
{
//заголовки для подключения
private $_headers = [
'Authorization: Bearer',
'Accept: application/json',
'Content-Type: application/json'
];
public function get($token, $url, array $data = [])
{
$curl = curl_init();
// возможно данные нужно передавать POST'ом
// curl_setopt($curl, CURLOPT_URL, $url.'?'.http_build_query($data));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers($token));
$out = curl_exec($curl);
if (!$out) {
echo 'Error: '.curl_error($curl);
}
curl_close($curl);
return $out;
}
private function headers($token)
{
$headers = $this->_headers;
$headers[0] .= ' '.$token;
return $headers;
}
}