Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at:
https://docs.zendframework.com/
Usage Scenarios - Zend_Ldap
Usage Scenarios
Basic CRUD operations
Retrieving data from the LDAP
Example #1 Getting an entry by its DN
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$hm = $ldap->getEntry('cn=Hugo Müller,ou=People,dc=my,dc=local');
/*
$hm is an array of the following structure
array(
'dn' => 'cn=Hugo Müller,ou=People,dc=my,dc=local',
'cn' => array('Hugo Müller'),
'sn' => array('Müller'),
'objectclass' => array('inetOrgPerson', 'top'),
...
)
*/
Example #2 Check for the existence of a given DN
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$isThere = $ldap->exists('cn=Hugo Müller,ou=People,dc=my,dc=local');
Example #3 Count children of a given DN
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$childrenCount = $ldap->countChildren(
'cn=Hugo Müller,ou=People,dc=my,dc=local');
Example #4 Searching the LDAP tree
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$result = $ldap->search('(objectclass=*)',
'ou=People,dc=my,dc=local',
Zend_Ldap_Ext::SEARCH_SCOPE_ONE);
foreach ($result as $item) {
echo $item["dn"] .
': ' .
$item['cn'][0] . PHP_EOL;
}
Adding data to the LDAP
Example #5 Add a new entry to the LDAP
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
Zend_Ldap_Attribute::setAttribute($entry, 'cn', 'Hans Meier');
Zend_Ldap_Attribute::setAttribute($entry, 'sn', 'Meier');
Zend_Ldap_Attribute::setAttribute($entry, 'objectClass', 'inetOrgPerson');
$ldap->add('cn=Hans Meier,ou=People,dc=my,dc=local', $entry);
Deleting from the LDAP
Example #6 Delete an existing entry from the LDAP
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$ldap->delete('cn=Hans Meier,ou=People,dc=my,dc=local');
Updating the LDAP
Example #7 Update an existing entry on the LDAP
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$hm = $ldap->getEntry('cn=Hugo Müller,ou=People,dc=my,dc=local');
Zend_Ldap_Attribute::setAttribute($hm, 'mail', 'mueller@my.local');
Zend_Ldap_Attribute::setPassword($hm,
'newPa$$w0rd',
Zend_Ldap_Attribute::PASSWORD_HASH_SHA1);
$ldap->update('cn=Hugo Müller,ou=People,dc=my,dc=local', $hm);
Extended operations
Copy and move entries in the LDAP
Example #8 Copy a LDAP entry recursively with all its descendants
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$ldap->copy('cn=Hugo Müller,ou=People,dc=my,dc=local',
'cn=Hans Meier,ou=People,dc=my,dc=local',
true);
Example #9
Move a LDAP entry recursively with all its descendants to a different subtree
$options =
array(/* ... */);
$ldap = new Zend_Ldap($options);
$ldap->bind();
$ldap->moveToSubtree('cn=Hugo Müller,ou=People,dc=my,dc=local',
'ou=Dismissed,dc=my,dc=local',
true);