title: ldap_bind() - Can't contact LDAP server
body: <p>I have a VM running CentOS 7 with the LAMP stack installed. On the VM I am trying to create an LDAPS connection to my domain controller which is a Windows 2008 R2 VM. The SSL certificate that I am using is self-signed and the CA has been added to the CentOS 7 CA trust.</p>
  
  <p>I am able to connect to the domain controller via ldapsearch. With ldapsearch debugging level set to 1, I am able to verify that my certificate is valid.</p>
  
  <pre><code>ex.) ldapsearch -H "ldaps://server.ad.com" -D "domain\user-name" -W -d 1
  </code></pre>
  
  <p>Whenever I try to use LDAPS via php, I receive ldap_connect() as success but the ldap_bind() always errors with -1 Can't contact LDAP server. Below is a code example:</p>
  
  <pre><code>&lt;?php
  
  define(LDAP_OPT_DIAGNOSTIC_MESSAGE,0x0032);
  echo "defined LDAP_OPT_DIAGNOSTIC_MESSAGE &lt;br /&gt;";
  
  $handle = ldap_connect("ldaps://server.ad.com:636");
  echo "called ldap_connect &lt;br /&gt;";
  $errorCode = ldap_errno( $handle );
  echo "error code: $errorCode &lt;br /&gt;";
  $errorMsg = ldap_error( $handle );
  echo "error message: $errorMsg &lt;br /&gt;";
  
  if (!$handle)
  {
      echo "ldap_connect method returned null &lt;br /&gt;";
  }
  else
  {
      echo "ldap_connect returned a handle! &lt;br /&gt;";
  }
  
  $bind = ldap_bind($handle, 'domain\user', 'password');
  echo "called ldap_bind &lt;br /&gt;";
  $errorCode2 = ldap_errno( $handle );
  echo "error code: $errorCode2 &lt;br /&gt;";
  $errorMsg2 = ldap_error( $handle );
  echo "error message: $errorMsg2 &lt;br /&gt;";
  
  if (!$bind)
  {
      echo "ldap_bind method returned null &lt;br /&gt;";
  }
  else
  {
      echo "ldap_bind returned a bind! &lt;br /&gt;";
  }
  
  if(ldap_get_option($handle, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error))
  {
      echo "Error binding to LDAP: $extended_error";
  }
  else
  {
      echo "Error bind to LDAP: No additional information is available.";
  }   
  ?&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>defined LDAP_OPT_DIAGNOSTIC_MESSAGE
  called ldap_connect
  error code: 0
  error message: Success
  ldap_connect returned a handle!
  called ldap_bind
  error code: -1
  error message: Can't contact LDAP server
  ldap_bind method returned null
  Error bind to LDAP: No additional information is available.
  </code></pre>
  
  <p>I feel like the "Can't contact LDAP server" is too generic of an error message, so I tried to add the LDAP_OPT_DIAGNOSTIC_MESSAGE (<a href="http://php.net/manual/en/function.ldap-bind.php" rel="nofollow">http://php.net/manual/en/function.ldap-bind.php</a> -first comment). But this does not seem to work.</p>
  
  <p>Any Ideas?</p>