Yes you can ping with php, here's an example:
function ping($host, $port, $timeout) {
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}
//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("xxx.it", 25, 10);
I'm sure this isn't enough to solve your problem but it will, at least, help you to determine if you can connect to the remote serve before continuing with the rest of the script.
You should also consider that the remote server maybe be blocking connections for your host.