PHP code example of xp-framework / ldap

1. Go to this page and download the library: Download xp-framework/ldap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xp-framework / ldap example snippets


use peer\ldap\LDAPConnection;
use util\cmd\Console;

$l= new LDAPConnection('ldap://ldap.example.com');
$l->connect();

$search= $l->search(
  'ou=People,dc=OpenLDAP,dc=Org', 
  '(objectClass=*)'
);
  
Console::writeLinef('===> %d entries found', $search->numEntries());
foreach ($search as $result) {
  Console::writeLine('---> ', $result->toString());
}

$l->close();

use peer\ldap\{LDAPConnection, LDAPEntry};

$l= new LDAPConnection('ldap://uid=admin,o=roles,dc=planet-xp,dc=net:[email protected]');
$l->connect();

with ($entry= $l->read(new LDAPEntry('uid=1549,o=people,dc=planet-xp,dc=net'))); {
  $entry->setAttribute('firstname', 'Timm');

  $l->modify($entry);
}

$l->close();

use peer\ldap\{LDAPConnection, LDAPEntry};

$l= new LDAPConnection('ldap://uid=admin,o=roles,dc=planet-xp,dc=net:[email protected]');
$l->connect();

with ($entry= new LDAPEntry('uid=1549,o=people,dc=planet-xp,dc=net')); {
  $entry->setAttribute('uid', 1549);
  $entry->setAttribute('firstname', 'Timm');
  $entry->setAttribute('lastname', 'Friebe');
  $entry->setAttribute('objectClass', 'xpPerson');

  $l->add($entry);
}

$l->close();

use peer\ldap\LDAPQuery;

$res= $ldap->searchBy(new LDAPQuery(
  'o=people,dc=planet-xp,dc=net',
  '(&(objectClass=%c)(|(username=%s)(uid=%d)))',
  'xpPerson',
  'friebe'
  1549
));