如何在 PHP 8 中获取分页 LDAP 查询并读取超过 1000 个条目?

How to get paged LDAP queries in PHP 8 and read more than 1000 entries?

我需要获取当前 LDAP 搜索返回的超过 1000 个条目。目前我 运行 在带有 IIS 和 PHP 7.4 的 Windows 服务器上,但很快就会升级到 8.0。

到目前为止我尝试过的是:

# Connect to the LDAP server
$ldap = ldap_connect("ldaps://my.ldap.server:636");
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$ldapBind = ldap_bind($ldap, "UserName", "Password");

# Do a search
$searchResult = ldap_search($ldap, "DC=something1,DC=something2", "LDAP query);
$countEntries += ldap_count_entries($ldap, $searchResult);
$info = ldap_get_entries($ldap, $searchResult);

# Process each found LDAP data row - this will be a maximum of 1000 rows
for($i=0; $i < $info["count"]; $i++) {
    # do something ...
}

这只会给我前 1000 行,但我至少需要阅读 30000 行。使用像 mail=a*mail=b* 这样的“命名查询”不是一个可行的解决方案,因为那里可能有超过 1000 个条目,所以我需要某种可信的分页方法——一页接一页。

我可以看到我很可能应该使用 LDAP controls 因为 ldap_control_paged_result 在 PHP 7.4 之后不再是一个选项但我真的不明白 - 如何使用它?

任何人都可以提供一些关于在这里做什么的提示吗? :-)

PHP documentation 中有一个示例(参见示例 #5),说明如何在 PHP 7.4+ 中从 LDAP 获取和分页数据。

官方文档中的以下代码片段已调整为每页 750 项的页面大小。

// $link is an LDAP connection

$cookie = '';

do {
    $result = ldap_search(
        $link, 'dc=example,dc=base', '(cn=*)', ['cn'], 0, 0, 0, LDAP_DEREF_NEVER,
        [['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => 750, 'cookie' => $cookie]]]
    );
    ldap_parse_result($link, $result, $errcode , $matcheddn , $errmsg , $referrals, $controls);
    // To keep the example short errors are not tested
    $entries = ldap_get_entries($link, $result);
    foreach ($entries as $entry) {
        echo "cn: ".$entry['cn'][0]."\n";
    }
    if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
        // You need to pass the cookie from the last call to the next one
        $cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
    } else {
        $cookie = '';
    }
    // Empty cookie means last page
} while (!empty($cookie));