希望下面的程式碼能幫助您使用 LDAP3 伺服器迴圈處理參考。我花了相當多的時間才弄清楚這東西是如何運作的,現在我已經成功地將它與 Active Directory 一起使用,以迴圈處理多個子網域。
特別感謝 Stig Venaas 幫助我入門。(最初的問題是 Windows 組建中缺少 ldap_parse_reference 函數。至少在我提交錯誤報告後,它現在可以在 Windows PHP 版本 5.1.0-DEV 和更高版本上運作)
function crawlRefs($user, $passw, $host, $dn, $port, $filter) {
//建立用於擷取參考的基本連線
$adConn = ldap_connect($host, $port) or die("系統無法連線!");
ldap_set_option($adConn, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("系統無法進行第一個協定選項設定!");
ldap_set_option($adConn, LDAP_OPT_REFERRALS, 0) or die ("系統無法進行第二個協定選項設定!");
$bd = ldap_bind($adConn, $user, $passw) or die ("系統無法繫結連線!");
$search = ldap_search($adConn, $dn, $filter);
//尋找參考
$ref = ldap_first_reference($adConn, $search);
while ($ref) {
if (ldap_parse_reference($adConn, $ref, $referrals)) {
while ($referral = array_shift($referrals)) {
echo $referral . "<br>\n";
}
}
$ref = ldap_next_reference($adConn, $ref);
}