title: PHP slow response when using external hostnames
body: <p>I have some problems with the exeuction time of my scripts in PHP on Windows (PHP 5.5.14 on IIS 8.5 using Plesk Panel 12). When my PHP scripts need to connect to external resources (mailservers, social media APIs, etc.), this takes some seconds, which is too long. This problem only occurs when I use a hostname (e.g. smtp.gmail.com). When I use the IP address of that server, the script only takes a few milliseconds to load. Thus, the problem seems to be in the DNS resolving.</p>
  
  <p>I found this thread about <a href="https://stackoverflow.com/questions/1113638/dns-lookup-in-php">DNS Lookup in PHP</a>, so I tried the following with PHPMailer:</p>
  
  <ol>
  <li>Use the hostname of the mailserver as host (slow)</li>
  <li>Use the IP-address of the mailserver as host (fast!)</li>
  <li>Let PHP do a DNS lookup and then use that IP-address as host (fast!):</li>
  </ol>
  
  <p>A strange thing is that the 3rd method only works for IPv4 addresses:</p>
  
  <pre><code>$result = dns_get_record("&lt;hostname of mailserver&gt;");
  foreach($result as $output):
      if($output['type'] == 'A'):
          $ip = $output['ip'];
      endif;
  endforeach;
  
  ....
  $mailer-&gt;Host = $ip;
  ....
  </code></pre>
  
  <p>When I try to use the IPv6 for that server (which resolves perfectly), PHPMailer says that it cannot connect to the server. </p>
  
  <pre><code>$result = dns_get_record("&lt;hostname of mailserver&gt;");
  foreach($result as $output):
      if($output['type'] == 'AAAA'):
          $ip = $output['ipv6'];
      endif;
  endforeach;
  
  ....
  $mailer-&gt;Host = $ip;
  ....
  </code></pre>
  
  <p>Of course, the first method (just using the hostname of a server) is the preferred method, as services like Google have a lot of IP-addresses that constantly change. Why does this method takes seconds to execute while doing a DNS lookup first and then using that IP only takes a few milliseconds?</p>