I'm trying to create a PHP script that will return some details of each member that is part of a specific group in our Active Directory.
I have no problem connecting and display the names (CN) of the group members but when it comes to displaying details such as telephone, email and username I'm stuck.
Here's my code I'm trying with. Can anyone see what I'm doing wrong?
<?php
$ldap_server = "AD_Server.domain.pri:389";
$auth_user = "user@domain.pri";
$auth_pass = "password";
$base_dn = "OU=IM Groups,OU=GLOBAL,DC=domain,DC=pri";
$filter = "(&(objectCategory=user)(memberOf=IM-ALL_USERS))";
// connect to server
if (!($connect=@ldap_connect($ldap_server))) {
die("Could not connect to ldap server");
}
// bind to server
if (!($bind = ldap_bind($connect, $auth_user, $auth_pass))) {
die("Unable to bind to server");
}
// search active directory
if (!($search = ldap_search($connect, $base_dn, $filter))) {
die("Unable to search ldap server");
}
$number_returned = ldap_count_entries($connect,$search);
$info = ldap_get_entries($connect, $search);
echo "The number of entries returned is ". $number_returned."<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "Name is: ". $info[$i]["givenname"][0]."<br>";
echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
echo "Email is: ". $info[$i]["mail"][0]."<br>";
echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";
}
?>
Worked it out using an excellent function created by Sam J Levy.
Here's the final code that worked.