Why is the internal energy of a real gas a function of pressure and temperature only?
Ideal gas
In thermodynamics, the fact that the energy of an ideal gas depends only on temperature is an experimental observation from the free expansion of a diluted gas (which is approximately ideal) 1. In statistical mechanics, it can be proven 2.
Ultimately, the reason is that the atoms of an ideal gas are non-interacting point particles. They only have kinetic energy, but don't "see" each other at all. The average kinetic energy is nothing else than the temperature of the ideal gas.
Therefore, no matter how much you decrease the volume of the box: since they don't interact, the energy will remain the same if the temperature (=average kinetic energy) is unchanged.
For more realistic models, you have an interaction potential $U$ which is usually assumed to be the sum of pairwise components that depend on the distance between particle $i$ and particle $j$,
$$U(\vec r_i, \dots, \vec r_N) = \sum_{i=1,j<i}^N u(|\vec r_i - \vec r_j|)$$
When the volume is reduced the average interparticle distance is reduced, and therefore, because of the presence of $U$, the energy will change even if $T$ is kept constant.
Real gas
In general the energy $E$ of a real gas will be a function of all the relevant thermodynamic parameters, which are $P,V$ and $T$:
$$E_{rg} = E(P,V,T)\tag{1}\label{1}$$
However, there is always an equation of state connecting $P,V$ and $T$:
$$f(P,V,T)=0\tag{2}\label{2}$$
An example is the van der Waals equation of state:
$$\left( P + \frac{a n^2}{V^2}\right) (V-nb) = nRT$$
You can solve \ref{2} for $V$, obtaining
$$V = g(P,T) \tag{3}\label{3}$$
Substituting \ref{3} in \ref{1}, you obtain
$$E(P,V,T)=E(P,g(P,T),T)= \tilde E(P,T)$$
So that $E$ only depends on two thermodynamic variables.
1 The experiment was first performed by Joule and it works like this: you let a diluted gas, initially confined in a side of a container, freely expand in the whole container. Assuming that the container is adiabatic (no heat exchange), the work done $W$ and the heat exchanged $Q$ are both zero. Therefore, from the first law, $\Delta E=0$: the energy is unchanged. The volume has changed, and also the pressure. Experimentally, it is observed that the temperature has not changed. You conclude therefore that $E$ must be a function of $T$ only. You can find this argument in E. Fermi, Thermodynamics, Chapter 2.
2 See for example K. Huang, Statistical Mechanics, Chapter 6.5