This completely works for me:
// inside hosts file:
// 10.128.0.23 test.domain.example
$socket = fsockopen('test.domain.example', 80);
fclose($socket);
I can ping that domain name and get a valid answer, and it runs a web server on port 80.
I read from your comment that your PHP is inside a virtual machine - then you should try if the ping does work there also, and if not: add the domain name to your hosts file as well. Virtual machines do not necessarily inherit the name resolving capabilities of their hosts.