I had this problem too. When I used ‘localhost’ for the host name, it failed. I used ‘127.0.0.1’ and it worked. I can only guess that php doesn’t use /etc/hosts for name resolution.
When I used ‘localhost’ for the host name, it failed. I used ‘127.0.0.1’ instead, and it worked. Go figure. My only guess is that php doesn’t use /etc/hosts for name resolution, so the name you use must be externally resolvable. That’s just a guess.