Fetching entries

Fetching entries – Retrieving entries directly or from a searchresult

Retrieving entries directly

You can retrieve directory entries in several ways, either directly or from a performed search request. If you want to fetch an entry directly, you need to know its absolute distinguished name (DN). To directly fetch an known entry from the directory server, you use Net_LDAP2's getEntry() method. It takes two parameters: The DN of the entry and the attributes you want to read from the entry. It returns a Net_LDAP2_Entry object if fetching worked, or a Net_LDAP2_Error object in case of a failure.

You may also check if the entry exists in the server before you fetch it. This can be achieved by Net_LDAP2's dnExists() which takes the DN to test and returns either true or false.

Fetching an entry directly

<?php
// Defining the DN we want to fetch;
// we want to select the given- and the surname
$dn 'cn=admin,o=example,dc=org';
$entry $ldap->getEntry($dn, array('gn''sn'));

// Error checking is important!
if (Net_LDAP2::isError($entry)) {
    die(
'Could not fetch entry: '.$entry->getMessage());
}
?>

Retrieving entries from a searchresult

The second way to retrieve entries is from a searchresult. As described in chapter "Search", you access the entries of a search result through the Net_LDAP2_Search-object resulting from Net_LDAP2's search() method. Each of the following methods return a Net_LDAP2_Error-object if something goes wrong, so remember to test for errors! You have several ways to read the entries:

Possible ways to fetch entries
Method of Net_LDAP2_Search Описание
entries() This returns the entries at once unsorted.
as_struct() This returns all entries as multidimensional array instead of Net_LDAP2_Entry-objects. The array keys of the first dimension are the DNs and the value is an array containing all attributes. The array keys of the second level are the attributes names; the value of the second level is an array containing all the attributes values. Note, that even if there are no or just one value, an array is present.
sorted() Use this if you want to get the entries at once but sorted. You can sort by several attributes which can contain multiple values. You can of course sort ascending (default) or descending - just pass the PHP constant SORT_ASC or SORT_DESC as second parameter.
sorted_as_struct() Like as_struct(), this returns the entries as multidimensional array, but in this case sorted. For parameters, see sorted()
shiftEntry() This returns one entry from the beginning of the search result. Since this returns FALSE if all entries are fetched, shiftEntry() is perfectly appropriate to get used inside a while-loop. Take care not to mix shiftEntry() and popEntry()!
popEntry() Exactly the same as shiftEntry, but returns the entry from the end of the searchresult. Again, be sure to not mix shiftEntry() and popEntry()!

To directly fetch an known entry from the directory server, you use Net_LDAP2's getEntry() method. It takes two parameters: The DN of the entry and the attributes you want to read from the entry. It returns a Net_LDAP2_Entry object if fetching worked, or a Net_LDAP2_Error object in case of a failure.

You may also check if the entry exists in the server before you fetch it. This can be achieved by Net_LDAP2's dnExists() which takes the DN to test and returns either true or false.

Fetching all entries from searchresult

<?php
// return all entries:
$entry $search->entries();
?>

Fetching all entries from searchresult: sorted

<?php
// return sorted by first 'sn' then 'gn', but descending:
$entry $search->sorted(array('sn''gn'), SORT_DESC);
?>

Fetching entries one by one inside a while loop

<?php
// return entries one by one:
while ( $entry $search->shiftEntry() ) {
    
// do something, like printing the DN of the entry;
    // in a real case, dont forget to test for errors!
    
echo "ENTRY: " $entry->dn();
}
?>

Retrieving entries via iteration (foreach)

Since Net_LDAP2 you are able to use PHPs Standard Library (SPL) to iterate over search results. This is done easily by just using the Net_LDAP2_Search search result object inside an foreach loop similar to an array. You may optionally retrieve the DN of each entry by the same mechanism you use to retrieve the key of an associative array.

Fetching entries via foreach()

<?php
foreach ($search as $dn => $entry) {
    
// do something:
    
$sn $entry->getValue('sn''single');
    echo 
"Fetched DN: $dn; Surname: $sn";
}
?>
    Поддержать сайт на родительском проекте КГБ