0

我是 PHP 新手,想用名为“mail”的 LDAP 属性填充变量 $email。我已经使用此代码来执行此操作:

$email = $info[$i]["mail"][0];

但是,我没有得到任何返回值。这是填充变量 $email 的正确语法吗?如果不是,有人可以告诉我如何使用 ldap 属性“mail”填充 $email 变量。

当我执行 echo 语句以打印出 ldap 属性时,我可以看到如下值:

名字:Jim 姓氏:Smith 电子邮件:jimsmith@mycompany.com

谢谢,非常感谢

代码如下:

<?php
   error_reporting(E_ALL);
   $connect;
   $ldap_host='xxxx.mycompany.com';
   $ldap_admin_dn='cn=xxxxx,ou=xxxxxx,ou=aaa,ou=administrators,dc=xxxx,dc=ca';
   $ldap_pass='#####';
   $ldap_basedn='ou=people, dc=xxxx, dc=com';
    if(!($connect = ldap_connect($ldap_host))){
         die("Could not connect to $ldap_host");
    }

    if(!($bind = ldap_bind($connect, $ldap_admin_dn, $ldap_pass))){
        die("Could not bind to $connect");
    }

    $netlinkID = "test";
    $search = ldap_search($connect, $ldap_basedn, "uid=$netlinkID");
    $info = ldap_get_entries($connect, $search);

    for ($i=0; $i<$info["count"]; $i++) {
       echo"<h3>Requestor Info</h3>";
       echo "<div class=\"col1\">First Name:" . "<span class=\"req-firstname\">" . $info[$i]["givenname"][0]. "</span></div>";
       echo "<div class=\"col1\">Last Name:" . "<span class=\"req-lastname\">" . $info[$i]["sn"][0]. "</span></div>";
       echo "<div class=\"col1\">Email: " . "<span class=\"req-email\">" . $info[$i]["mail"][0]. "</span></div>";

     //How to populate variable email to get value from the LDAP Mail attribute
     $email = $info[$i]["mail"][0];
     }

ldap_close($connect);
?>
4

1 回答 1

0

尝试做:

...
$info = ldap_get_entries($connect, $search);
print_r($info);
for ($i=0; $i<$info["count"]; $i++) {
...

还可以尝试在循环中首先分配您的名字、sn 和邮件:

for ($i=0; $i<$info["count"]; $i++) {
extract($info[$i]);
$email = $mail[0];
   echo"<h3>Requestor Info</h3>";
   echo "<div class=\"col1\">First Name:" . "<span class=\"req-firstname\">" . $givenname[0]. "</span></div>";
   echo "<div class=\"col1\">Last Name:" . "<span class=\"req-lastname\">" . $sn[0]. "</span></div>";
   echo "<div class=\"col1\">Email: " . "<span class=\"req-email\">" . $email. "</span></div>";

 //How to populate variable email to get value from the LDAP Mail attribute
 }

看看你得到什么结果。

于 2013-07-05T23:57:48.807 回答