Hello, I am trying to list all sub OU's from active directory. I can pull all the parent OU's but not to sure how to drill down to get the sub OUs. Below code will pull the parent OU's but I am not sure if ldap_list can drill down. any help would be appreciated.

<?php
$name = "admin";
$pass = "foobar";

 $adServer = "127.0.01";
 $ldapconn = ldap_connect($adServer) or die("Could not connect to LDAP server.");
 ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("Could not set ldap protocol");
 ldap_set_option($ad, LDAP_OPT_REFERRALS, 0) or die ("Could not set option referrals");

 $account = $name;
 $password = $pass;
 $ldaprdn = $account."@foobar.com";
 $ldappass = $password;

 if ($ldapconn) {
  $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass)  or die("Couldn't bind to AD!");
 }

$dn = "ou=Agencies,dc=foobar,dc=com";
$justthese = array("ou");

$sr = ldap_list($ldapconn, $dn, "ou=*", $justthese);

$info = ldap_get_entries($ldapconn, $sr);

// List OUs
for ($i=0; $i < $info["count"]; $i++) {
    echo $info[$i]["ou"][0]."<br>";
}

 ldap_free_result($sr);
 ldap_unbind($ldapconn);
?>

    figured it out, have to use ladap_search .. code below will list all OUs and sub OUs into an array

    $name = "username";
    $pass = "password";
    
     $adServer = "127.0.0.1";
     $ldapconn = ldap_connect($adServer) or die("Could not connect to LDAP server.");
     ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("Could not set ldap protocol");
     ldap_set_option($ad, LDAP_OPT_REFERRALS, 0) or die ("Could not set option referrals");
    
     $account = $name;
     $password = $pass;
     $ldaprdn = $account."@foobar.com";
     $ldappass = $password;
    
     if ($ldapconn) {
      $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass)  or die("Couldn't bind to AD!");
     }
    
     $dn = "ou=Agencies,dc=foobar,dc=com";
     $filter="(objectClass=organizationalunit)";
     $justthese = array("dn", "ou");
     $sr=ldap_search($ldapconn, $dn, $filter, $justthese);
     $info = ldap_get_entries($ldapconn, $sr);
    
    for ($i=0; $i < $info["count"]; $i++) {
            echo $info[$i]["dn"]."<br>";
    }
     ldap_free_result($sr);
     ldap_unbind($ldapconn);
    
      Write a Reply...