PHP Conference Japan 2024

ldap_get_attributes

(PHP 4, PHP 5, PHP 7, PHP 8)

ldap_get_attributes從搜尋結果項目取得屬性

說明

ldap_get_attributes(LDAP\Connection $ldap, LDAP\ResultEntry $entry): 陣列

從搜尋結果的項目中讀取屬性和值。

在目錄中找到特定項目後,您可以使用此呼叫找出該項目所包含的資訊。如果您開發的應用程式需要「瀏覽」目錄項目,或者您不知道目錄項目的結構,則可以使用此呼叫。在許多應用程式中,您會搜尋特定的屬性,例如電子郵件地址或姓氏,而不在乎其他資料的內容。

return_value["count"] = number of attributes in the entry
return_value[0] = first attribute
return_value[n] = nth attribute

return_value["attribute"]["count"] = number of values for attribute
return_value["attribute"][0] = first value of the attribute
return_value["attribute"][i] = (i+1)th value of the attribute

參數

ldap

一個 LDAP\Connection 實例,由 ldap_connect() 返回。

entry

一個 LDAP\ResultEntry 實例。

返回值

返回一個多維陣列,包含完整的項目資訊。

更新日誌

版本 說明
8.1.0 ldap 參數現在需要一個 LDAP\Connection 實例;先前需要一個有效的 ldap link 資源
8.1.0 entry 參數現在需要一個 LDAP\ResultEntry 實例;先前需要一個有效的 ldap result entry 資源

範例

範例 #1 顯示特定目錄項目所包含的屬性列表

<?php
// $ds 是一個有效的 LDAP\Connection 實例,用於連接目錄伺服器

// $sr 是一個有效的搜尋結果,來自先前呼叫的
// 其中一個 ldap 目錄搜尋函式

$entry = ldap_first_entry($ds, $sr);

$attrs = ldap_get_attributes($ds, $entry);

echo
$attrs["count"] . " 此項目包含的屬性數量:<p>";

for (
$i=0; $i < $attrs["count"]; $i++) {
echo
$attrs[$i] . "<br />";
}
?>

參見

新增註釋

使用者貢獻的註釋 6 則註釋

4
kop at meme dot com
21 年前
建議在 ldap_get_attributes() 的結果上使用 array_change_key_case() 函式,這樣你的程式就能忽略屬性名稱的大小寫,就像 ldap 本身一樣。(你不希望 ldap_get_attributes() _總是_ 將大小寫扁平化,因為你需要一種方法以漂亮的格式獲取屬性名稱來顯示給使用者。)
1
allie at pajunas dot com
22 年前
這個函式建立的陣列類似於 ldap_get_entries() 建立的陣列,但在建立陣列鍵值時,它會不一致地更改屬性的大小寫。

ldap_get_entries() 會在用屬性作為鍵值之前將所有屬性轉換為小寫,但這個函式似乎會保留原樣。

如果你遇到問題,請使用 print_r 檢查並確保你使用了正確大小寫的陣列鍵值。例如,你可能需要使用 "objectClass" 而不是 "objectclass"。
0
software at inebria dot com
23 年前
在 PHP 4.0.5 版本中,ldap_get_attributes 函式無法處理二進位資料。如果你要從 LDAP 伺服器擷取 JPEG 影像,請改用 ldap_get_values_len 函式。
-2
php dot net at hiddemann dot org
20 年前
請注意,ldap_get_attributes 會從 LDAP 伺服器擷取屬性和值。根據儲存在項目中的資料,以下程式碼的執行速度可能比範例 1 中的程式碼快上數十倍(或者更快,但總體來說更快)

<?php
// $ds 是目錄的連結識別碼

// $sr 是先前呼叫 ldap 目錄搜尋呼叫的有效搜尋結果

$entry = ldap_first_entry($ds, $sr);

$attrs = array();
$attribute = ldap_first_attribute($ds,$entry,$identifier);
while (
$attribute) {
$attrs[] = $attribute;
$attribute=ldap_next_attribute($ds,$entry,$identifier);
}

echo
count($attrs) . " 此項目擁有的屬性數量:<p>";

for (
$i=0; $i<count($attrs); $i++) {
echo
$attrs[$i] . "<br />";
}
?>

你可以使用 "microtime" 函式檢查時間差異。
-1
Snezko Snezak
18 年前
以下程式碼與函式可以從特定 DN 的所有項目中擷取所有屬性。或許不是執行效率最佳的方式,但它可以運作。

$entry = ldap_first_entry($ds, $sr);
write_attr($entry,$ds);
for ($i = 0; $i < $n_entries; $i++){
$entry = ldap_next_entry($ds, $entry);
write_attr($entry,$ds);
}

function write_attr($entry,$ds){
$attrs = ldap_get_attributes ($ds, $entry);
for ($j = 0; $j < $attrs["count"]; $j++){
$attr_name = $attrs[$j];
$attrs["$attr_name"]["count"] . "\n";
for ($k = 0; $k < $attrs["$attr_name"]["count"]; $k++) {
echo ">>>>>>";
echo $attr_name.": ".$attrs["$attr_name"][$k]."\n";
}
}
}
-2
dunc at rumbletum dot org
17 年前
我花了不少時間才搞懂如何讀取操作屬性,例如建立和修改時間戳記。

這個函式解決了這個問題....

function get_entry_system_attrs( $ds, $dn, $deref=LDAP_DEREF_NEVER )
{
$conn = $ds;
$attrs = array( 'creatorsname', 'createtimestamp', 'modifiersname',
'structuralObjectClass', 'entryUUID', 'modifytimestamp',
'subschemaSubentry', 'hasSubordinates', '+' );
$search = @ldap_read( $conn, $dn, '(objectClass=*)', $attrs, 0, 0, 0, $deref );
if( ! $search )
return false;
$entry = ldap_first_entry( $conn, $search );
if( ! $entry)
return false;
$attrs = ldap_get_attributes( $conn, $entry );
if( ! $attrs )
return false;
if( ! isset( $attrs['count'] ) )
return false;
$count = $attrs['count'];
unset( $attrs['count'] );
$return_attrs = array();
for( $i=0; $i<$count; $i++ ) {
$attr_name = $attrs[$i];
unset( $attrs[$attr_name]['count'] );
$return_attrs[$attr_name] = $attrs[$attr_name];
}
return $return_attrs;
}
To Top