PHP 8.3.4 Released!

ldap_get_attributes

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

ldap_get_attributes検索結果エントリから属性を得る

説明

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

検索結果エントリから属性と値を読み込みます。

ディレクトリに特定のエントリを置いている場合、この関数をコールする ことにより、そのエントリに関して保持されている情報得ることができます。 ディレクトリエントリを "ブラウズ" するか、ディレクトリエントリの 構造が未知であるアプリケーションにおいてこの関数を使用します。 多くのアプリケーションにおいては、電子メールアドレスや姓のような 特定の属性を検索するため、保持されている他のデータにどんなものがあるか ということは問題にならないと思われます。

return_value["count"] = そのエントリの属性の数
return_value[0] = 最初の属性
return_value[n] = n 番目の属性

return_value["attribute"]["count"] = その属性に関する値の数
return_value["attribute"][0] = その属性に関する最初の値
return_value["attribute"][i] = その属性に関する (i+1) 番目の値

パラメータ

ldap

ldap_connect() が返す LDAP\Connection クラスのインスタンス。

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"] . " attributes held for this entry:<p>";

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

参考

add a note

User Contributed Notes 6 notes

up
4
kop at meme dot com
20 years ago
It's good practice to use array_change_key_case() on the result of ldap_get_attributes() so your program can ignore case in attribute names just like ldap itself does. (You wouldn't want ldap_get_attributes to _always_ flatten the case because you need a way to get the attribute names in a pretty format for display to the user.)
up
1
allie at pajunas dot com
21 years ago
The array created by this function is similar to the on from ldap_get_entries() but when it creates array keys it alters the attribute's case inconsistently.

ldap_get_entries() lowercases all of the attributes before keying the array with them, but this function appears to leave things as they are.

If you're having problems, do a print_r and make sure you're using correct case for array keys. For example, you might need to use "objectClass" and not "objectclass".
up
0
software at inebria dot com
22 years ago
As of PHP 4.0.5, the ldap_get_attributes function does not work with binary data. If you're fetching a JPEG from an LDAP server, use ldap_get_values_len instead.
up
-2
php dot net at hiddemann dot org
19 years ago
Note that ldap_get_attributes fetches attributes AND values from the LDAP server. Depending on the data stored in the entry, the following code might work a dozen times faster (or even better, but faster in general) than the code in Example 1:

<?php
// $ds is the link identifier for the directory

// $sr is a valid search result from a prior call to
// one of the ldap directory search calls

$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) . " attributes held for this entry:<p>";

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

You may want to check the time difference with the function "microtime".
up
-1
Snezko Snezak
17 years ago
Code and function to extract all attributes from all entryes in a certain DN. Maybe not the most timewise efficient but it works.

$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";
}
}
}
up
-2
dunc at rumbletum dot org
16 years ago
I spent quite a while scratching my head about how to read operational attributes such as create and modify timestamps.

This function solved it....

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