You're not issuing the correct "Host: " HTTP header, so I assume there is a problem with one of the following lines:
$varnishhost = 'Host: ' . $_SERVER['SERVER_NAME'];curl_setopt($curl, CURLOPT_ENCODING, $varnishhost);