<?php 


class Request{
    private $apiKey;
    private $environment;

    public function __construct($apiKey){
        $this->apiKey = $apiKey;
    }

    public function makeRequest($method, $url, $body){
        $ch	= curl_init();

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, true);

        switch (strtolower($method))
        {
            case 'post':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
				if ($body != null)
                {
					curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
				}
                break;
            case 'get':
                break;
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type: application/json',
            'Simulator: EXTERNAL',
            'Authorization: Basic ' . $this->apiKey
        ));
        curl_setopt($ch, CURLOPT_URL, $url);

        $data			= curl_exec($ch);
        $info			= curl_getinfo($ch);
		
        $response_headers = substr($data, 0, $info['header_size']);
        $response_body = substr($data, $info['header_size']);

		curl_close($ch);
        
        $response = array
        (
            'headers'	=> $response_headers,
            'body'		=> $response_body,
            'info'		=> $info,
        );

        //var_dump($response);die();

        if($response['info']['http_code'] && $response['info']['http_code'] > 201 && $response['body']){
            require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/NetellerException.php";
            //var_dump('222');die();
            throw new NetellerException($response['body']);
        }
        return $response;
    }
}
