読み取り操作については、PHP は並列検索と呼ばれる機能をサポートしています。これは思ったほど単純ではありませんが、1 回の操作で目的の結果を得ることができます。
$links = array($link, $link); // yes, two references to the same link
$DNs = array(
'OU=Users,OU=NA1,DC=corp,DC=pvt',
'OU=Contacts,OU=NA1,DC=corp,DC=pvt'
);
$filter = 'attr=val';
// a regular call to ldap_search()
// only now, $results is and array of result identifiers
$results = ldap_search($links, $DNs, $filter);
これを次のような関数にラップして、呼び出しをより簡単にすることができます。
function ldap_multi_search($link, array $dns, $filter, array $attributes = null, $attrsonly = null, $sizelimit = null, $timelimit = null, $deref = null)
{
$dns = array_values($dns);
$links = array_fill(0, count($dns), $link);
$results = ldap_search($links, $dns, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref);
$retVal = array();
foreach ($results as $i => $result) {
if ($result === false) {
trigger_error('LDAP search operation returned error for DN ' . $dns[$i], E_USER_WARNING);
continue;
}
$entries = ldap_get_entries($result);
unset($result['count']); // we'll calculate this properly at the end
$retVal = array_merge($retVal, array_values($entries));
}
$entries['count'] = count($entries);
return $entries;
}