如何在CURL中处理服务器的连接重置

I am making a curl request to a remote server (the Vebra Api) that requires token authentication. From what I can gather the server will only allow one token request an hour. I am trying to discern whether my current token is currently valid.

My approach to this is to make a request and check whether the status is 200 or 401. If I have a valid token I can successfully check the curl info with curl_getinfo($ch) for the correct status code. If my token is invalid however the script terminates without me being able to handle the error - Firefox reports that The connection was reset before I can handle the error or process any other code.

Is this an issue with my code or perhaps the server? Might there be a way to tell the curl function to call some function in this scenario?

Code as follow:

// an invalid token
$token = '1sdfsdfds1RQUlJTTU1GRVdVT0tYUkJsdfsdfsdfdsfsdSEg=';

//Initiate a new curl session
$ch = curl_init($url);

//Don't require header this time as curl_getinfo will tell us if we get HTTP 200 or 401
curl_setopt($ch, CURLOPT_HEADER, 0); 

//Provide Token in header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '. $token ) ); 

// Tell curl to return a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Execute the curl session
$curlResp = curl_exec($ch);

// -----------------------------------------------------------------------------
// with invalid token script ends here with no chance for me to handle the error
// such as below
// -----------------------------------------------------------------------------

if ($curlResp === FALSE) {
    die('yarrrghhh! :(');
    //throw new Exception(); 
}

//Store the curl session info/returned headers into the $info array
$info = curl_getinfo($ch);

//Check if we have been authorised or not
if($info['http_code'] == '401') {

    echo 'Token Failed';

    var_dump($info);
    var_dump($curlResp);

} 
elseif ($info['http_code'] == '200') {

    echo 'Token Worked';

    var_dump($info);
    var_dump($curlResp);

}

//Close the curl session
curl_close($ch);

I think simply $curlResp is a string

try using

if ($curlResp == FALSE) {
die('yarrrghhh! :(');
//throw new Exception(); 
}

I've tested this code and via php-cli and a var_dump on it returns a string .. it's quite strange as php documentation says different things