What potential issues can arise when trying to connect to LDAP over SSL in PHP?

When trying to connect to LDAP over SSL in PHP, potential issues can arise due to misconfigured SSL certificates, incorrect LDAP server settings, or firewall restrictions. To solve this, ensure that the LDAP server is configured to accept SSL connections, verify that the SSL certificate is valid and trusted, and check for any firewall rules blocking the connection.

<?php
$ldapServer = 'ldaps://ldap.example.com';
$ldapPort = 636;

$ldapConn = ldap_connect($ldapServer, $ldapPort);

if (!$ldapConn) {
    die("Could not connect to LDAP server");
}

ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapConn, LDAP_OPT_REFERRALS, 0);

$ldapBind = ldap_bind($ldapConn, 'cn=admin,dc=example,dc=com', 'password');

if (!$ldapBind) {
    die("Could not bind to LDAP server");
}

echo "Connected to LDAP server over SSL";
ldap_close($ldapConn);
?>