In the previous example the
$ds = ldap.myserver.com // your ldap server
should be
$ds = ldap_connect( "ldap.myserver.com" ) ; // your ldap server
ldap_read
(PHP 4, PHP 5)
ldap_read — エントリを読み込む
説明
指定したフィルタ filter を使用し、スコープ LDAP_SCOPE_BASE でディレクトリを検索します。これは、 ディレクトリからエントリを読み込むことと同じ意味です。
パラメータ
- link_identifier
-
ldap_connect() が返す LDAP リンク ID。
- base_dn
-
ディレクトリのベース DN。
- filter
-
空のフィルタは指定できません。 このエントリに関する全ての情報を完全に取得したい場合は、 objectClass=* というフィルタを使用してください。 ディレクトリサーバーで使用されるエントリの型が分かっている場合、 objectClass=inetOrgPerson のように適切なフィルタを使用することができます。
- attributes
-
必要な属性を、 array("mail", "sn", "cn") のような通常の PHP 文字列配列で保持します。 "dn" は要求された属性の型によらず常に返されることに注意してください。
このパラメータを使用すると、デフォルトの動作よりもかなり効率的になります (デフォルトでは、すべての属性とその値を返します)。 したがって、これを使用することを推奨します。
- attrsonly
-
属性の型のみを取得したい場合は 1 を設定します。 属性の型および値の両方を取得したい場合は 0 を設定します (これがデフォルトの挙動です)。
- sizelimit
-
取得するエントリ数の制限を設定します。 0 は無制限であることを表します。
注意:
このパラメータは、サーバ側で事前に設定されている sizelimit を上書きすることはできません。それ以下の値を指定することはできます。
ディレクトリサーバのホストによっては、 事前に設定された数以上のエントリを返さないようになっているものもあります。 この場合、サーバでは、それが結果セットのすべてではないことを通知します。 このパラメータでエントリ数を制限した場合にも、同じことが起こります。
- timelimit
-
検索に要する最大秒数を設定します。 これを 0 にすると無制限であることを表します。
注意:
このパラメータは、サーバ側で事前に設定されている timelimit を上書きすることはできません。それ以下の値を指定することはできます。
- deref
-
検索時のエイリアスの扱いについて指定します。 以下のいずれかとなります。
- LDAP_DEREF_NEVER - (デフォルト) エイリアスは参照されません。
- LDAP_DEREF_SEARCHING - エイリアスを参照しますが、検索のベースオブジェクト上にいるときは参照しません。
- LDAP_DEREF_FINDING - エイリアスの参照は、ベースオブジェクト上にいて検索中でない場合に行われます。
- LDAP_DEREF_ALWAYS - エイリアスを常に参照します。 always.
返り値
検索結果 ID を返します。エラーの場合は、FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 4.0.5 | 並列検索のサポートが追加されました。詳細は ldap_search() を参照ください。 |
| 4.0.2 | attrsonly、sizelimit、 timelimit および deref が追加されました。 |
Clarification of the ldap_read command syntax:
If you just want to pull certain attributes from an object and you already know it's dn, the ldap_read command can do this as illustrated below. It will be less overhead than ldap_search.
The string base_dn which is normally used to set the top context for a recursive ldap_search is used slightly differently with this command. It is used to specify the actual object with the full dn. (Hopefully this saves someone else a couple hours trying this command out.)
<?php
$ds = ldap.myserver.com // your ldap server
$dn = "cn=username,o=My Company, c=US"; //the object itself instead of the top search level as in ldap_search
$filter="(objectclass=*)"; // this command requires some filter
$justthese = array("ou", "sn", "givenname", "mail"); //the attributes to pull, which is much more efficient than pulling all attributes if you don't do this
$sr=ldap_read($ds, $dn, $filter, $justthese);
$entry = ldap_get_entries($ds, $sr);
echo $entry[0]["mail"][0] . "is the email address of the cn your requested";
echo $entry[0]["sn"][0] . "is the sn of the cn your requested";
ldap_close($ds);
?>
This prints out the specified users mail and surname for example.
In addition to the above you can also use ldap_list for a ONE scope LDAP search.
PHP does not conform to the RFC's in that it does not use scope parameters. Instead use ldap_read for a scope of BASE, ldap_list for ONE and ldap_search for SUB.
This differs from ldap_search() by not recursing down to sub-entries. if you know the dn of the item you're looking for and only want info on that entry, use ldap_read() and pass it the full dn of the item you want.
It also seems that you'd alway want something like objectclass=* for the filter, since you're only searching on one entry.
