What are the best practices for handling HTTP response codes like 403, 404, and 50x when making requests to a remote server in PHP?

When making requests to a remote server in PHP, it is important to handle HTTP response codes like 403 (Forbidden), 404 (Not Found), and 50x (Server Errors) appropriately to ensure smooth functioning of the application. To handle these response codes, you can use try-catch blocks and switch statements to identify and handle each specific response code accordingly.

try {
    // Make the HTTP request to the remote server
    // Handle the response code
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    switch ($httpCode) {
        case 403:
            // Handle Forbidden response code
            break;
        case 404:
            // Handle Not Found response code
            break;
        case 500:
        case 502:
        case 503:
        case 504:
            // Handle Server Error response codes
            break;
        default:
            // Handle other response codes
            break;
    }
} catch (Exception $e) {
    // Handle any exceptions that may occur during the request
    echo 'Error: ' . $e->getMessage();
}