Wrapper class for calling external API I have a PHP cronjob script. I need to send requests and get responses from some external API. 

So, there is code for the API class. In this class, I connect to the API and  call public methods of this class from another class.

class Api {
private $authApi;
private $logger;

function __construct()
{
    global $_AUTH_API;
    $this-&gt;authApi = $_AUTH_API;
    $this-&gt;logger = new CustomLogs();
}

public function getBrands()
{
    $options = [
        'request' =&gt; 'get_brands',
    ];

    $brands = $this-&gt;requestApi($options);

    return $brands-&gt;rows;
}

public function get****() {}

/**
 * @param $data
 *
 * @return array|mixed|object
 * @throws Exception
 */
private function requestApi($data)
{
    $data = $this-&gt;authApi + $data;

    $data = [
        'data' =&gt; json_encode($data)
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.com/api/");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $answer = json_decode(curl_exec($ch));
    curl_close($ch);

    if (empty($answer)) {
        $this-&gt;logger-&gt;log-&gt;error('Empty answer from API');
        throw new Exception('Empty answer from API');
    } else {
        if ($answer-&gt;success != true) {
            $this-&gt;logger-&gt;log-&gt;error('API error: ', (array) $answer );
            throw new Exception('API error: ' . $answer-&gt;message  );
        } else {
            return $answer;
        }
    }
}
}


How can different API responses be handled correctly? What should be written in Logs? Do I need to throw Exceptions? What should be returned to external code? (i.e. in getBrands())

What would you change in this code?
First of all, it's a job for at least three separate functions (that should 
belong to the different classes at best, but that would be a different story), each throwing its own exceptions.

protected function sendRequest($data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this-&gt;apiUrl);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $data = curl_exec($ch);
    if ($error = curl_error($ch)) {
        throw new Exception($error);
    }   
    curl_close($ch);
    return $data;
}
protected function jsonDecode($json)
{
    $data = json_decode($json);
    if (json_last_error()) {
        throw new Exception(json_last_error_msg());
    }
    return $data;
}
protected function requestApi($data)
{
    $data = $this-&gt;authApi + $data;
    $data = [
        'data' =&gt; json_encode($data)
    ];
    $response = $this-&gt;sendRequest($data);
    $answer = $this-&gt;jsonDecode($response);

    if (isset($answer-&gt;success) &amp;&amp; $answer-&gt;success == true))) {
        return $answer;
    }
    if (isset($answer-&gt;message) {
        throw new \Exception('API error: ' . $answer-&gt;message  );
    } 
    throw new \Exception("Unknown API response: $response" );
}


As of the logger, I would call it in the exception handler/catch block and thus avoid repetitive calls.