The Ldap Library

The LDAP library provides means to connect to and use an LDAP server.

Installation

1
php composer.phar require "korowai/ldaplib:dev-master"

Basic Usage

Ldap Library provides Ldap class to authenticate and query against an LDAP server.

1
use Korowai\Lib\Ldap\Ldap;

An instance of Ldap may be easily created with Ldap::createWithConfig().

1
$ldap = Ldap::createWithConfig(['uri' => 'ldap://ldap-service']);

To establish connection and authenticate, the bind() method can be used.

1
2
/* Bind to 'cn=admin,dc=example,dc=org' using password 'admin'. */
$ldap->bind('cn=admin,dc=example,dc=org', 'admin');

Once bound, we can search in the database with search() method.

1
2
/* The returned result implements ResultInterface. */
$result = $ldap->search('ou=people,dc=example,dc=org', 'objectclass=*');

The $result object returned by search() provides access to entries selected by the query. It ($result) implements the ResultInterface which, in turn, includes the standard IteratorAggregate interface. This means, you may iterate over the result entries in a usual way

1
2
3
foreach($result as $dn => $entry) {
  print($dn . " => "); print_r($entry->getAttributes());
}

Alternatively, an array of entries can be retrieved with a single call to getEntries() method

1
$entries = $result->getEntries();

By default, entries’ distinguished names (DN) are used as array keys in the returned $entries

1
$entry = $entries['uid=jsmith,ou=people,dc=example,dc=org'];

Each entry is an Entry object and contains attributes. It can me modified in memory

1
$entry->setAttribute('uidnumber', [1234]);

Note

The Ldap library uses lower-cased keys to access entry attributes. Attributes in Entry are always array-valued.

Once modified, the entry may be written back to the LDAP database with update() method.

1
$ldap->update($entry);