Dreamscapes / Ldap-Core

Object encapsulation of PHP's native ldap functions
dreamscapes.github.io/Ldap-Core
BSD 3-Clause "New" or "Revised" License
10 stars 1 forks source link

Paged search #3

Closed Furgas closed 9 years ago

Furgas commented 9 years ago

How should I, using this library, approach searching with pagination as shown in "Example _#_2 LDAP pagination" on http://php.net/manual/en/function.ldap-control-paged-result.php ?

robertrossmann commented 9 years ago

That's simple! You can request the data to be returned to you in pages by first enabling paged responses before you send your search query.

Once you have a response, you can extract the cookie from it via this method. You will need to use the cookie on subsequent requests as the last parameter of the pagedResult() method.

So the whole process looks like this:

  1. Call resultResponse() with no cookie set (you do not have one yet)
  2. Perform search query
  3. Extract cookie from response (if any!) using pagedResultResponse()
  4. If there is cookie, repeat process from step 1, but set the cookie to the cookie you just got
  5. If there is no cookie in the response, your dataset is complete
Furgas commented 9 years ago

Thanks. Before I asked the questions, I tried to search for ldap_control_paged_result_response with github search but found nothing.

The paged search process calls for some helper method.

robertrossmann commented 9 years ago

It's here. :)

Github fails to find this function call because of the @ before it. Searching for _\@ldap_control_paged_resultresponse does the trick.

jrjohnson commented 9 years ago

After some messing around I ended up with this code which seems to work.

$results = [];
$cookie = '';
 do {
     $ldap->pagedResult(1000, false, $cookie);
     $response = $ldap->search($this->ldapSearchBase, $filter, $attributes);
     $arr = $response->getEntries();
     unset($arr['count']);
     $results = array_merge($results, $arr);
     $pagedArray = $response->pagedResultResponse();
     $cookie = !empty($pagedArray['cookie'])?$pagedArray['cookie']:false;
 } while ($cookie);
robertrossmann commented 9 years ago

Optionally you can do

$cookie = $response->pagedResultResponse("cookie");

Which will give you just the cookie.