What are the potential reasons for a "Fatal error: UnableToConnectException" when using PHP to connect to a VPS via SSH?

The "Fatal error: UnableToConnectException" in PHP when trying to connect to a VPS via SSH could be caused by incorrect SSH credentials, firewall restrictions, or network issues. To solve this issue, double-check the SSH credentials, ensure that the server allows SSH connections, and verify that the network connection is stable.

<?php
use phpseclib\Net\SSH2;

require 'vendor/autoload.php';

$ssh = new SSH2('example.com');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('ls -la');
?>