How can data be queried from an LDAP server using PHP, specifically with the ldap_search() function?

To query data from an LDAP server using PHP, specifically with the ldap_search() function, you need to establish a connection to the LDAP server, bind to it with appropriate credentials, and then perform a search using ldap_search(). The search results can be retrieved using ldap_get_entries().

// Connect to LDAP server
$ldapconn = ldap_connect("ldap.example.com");

// Bind to LDAP server
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");

// Search for entries
$ldapsearch = ldap_search($ldapconn, "dc=example,dc=com", "(objectclass=*)");

// Get search results
$ldapentries = ldap_get_entries($ldapconn, $ldapsearch);

// Print search results
print_r($ldapentries);