1

I have a LDAP server where I have all registered users. There is over than 3000 users and LDAP has 1000 entries limit for one search result. I need to have all users in one 'array' for json and administration and so on.

There is my code for search result over than 1000 entries:

$bind = ldap_bind($ds, $dn, $ldapconfig['password']);
$pageSize = 100;
$cookie = '';
$i = 0;
$attributes = array("email", "verificationHash", "gn", "sn");

do {
    ldap_control_paged_result($ds, $pageSize, true, $cookie);
    $filter = '(&(objectClass=Client))';
    $result  = ldap_search($ds, $baseDN, $filter, $attributes);
    $entries = ldap_get_entries($ds, $result);

    array_shift( $entries );

    $usersJSON = json_encode( $entries );

    header('Content-Type: application/json');
    echo $usersJSON; // return all users from ldap (over 3000)

    ldap_control_paged_result_response($ds, $result, $cookie);

} while($cookie !== null && $cookie != '');

// return array for javascript
$output = json_encode(
    array(
        'status' => true,
        'text' => 'Something',
        'data' => $entries
    )
);
die($output);

On front-end I need whole array but I get just first 100 entries. How can I split that result of cycles to one variable array?

Martin Jinda
  • 488
  • 10
  • 26
  • Did you somewhere call ```ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);```? Paged results are only available with LDAPv3. – heiglandreas Mar 21 '16 at 20:55
  • Yes, I have. It works prefect, it returns all results from LDAP, but I need these results in one array. – Martin Jinda Mar 22 '16 at 09:25
  • That `do` cycle did about 5 cycles, every cycle return ~100 entries. Every `echo` these entries print. How can I save these 5 cycles save to one array? – Martin Jinda Mar 22 '16 at 09:32

1 Answers1

0

to me it looks like you are overwriting your $entries array on each iteration with the new data. So I'd do something like this:

…
$entries = array();
do {
    …
    $entries = array_merge(ldap_get_entries($ds, $result), $entries);
    …
} while(…)
unset($entries['count']);
…

array_merge to the rescue ;)

heiglandreas
  • 3,803
  • 1
  • 17
  • 23