如何从Active Directory中获取数据作为JSON?

时间:2017-02-15 06:01:38

标签: php html json ajax active-directory

我有以下PHP文件,我试图通过该文件进行ajax调用并从JSON数组中获取所需的数据:



<?php
$username   = 'xxxxxxxxxxxx';
$password   = 'xxxxxxx';
$server = 'ldap://xxxxxxx';
$domain = '@asia.xxxxxxxx.com';
$port       = 389;

$ldap_connection = ldap_connect($server, $port);

if (! $ldap_connection)
{
    echo '<p>LDAP SERVER CONNECTION FAILED</p>';
    exit;
}

// Help talking to AD
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);

$ldap_bind = @ldap_bind($ldap_connection, $username.$domain, $password);

if (! $ldap_bind)
{
    echo '<p>LDAP BINDING FAILED</p>';
    exit;
}
else
{
 echo 'login successful';
}

$base_dn = "OU=Employees,OU=Accounts,OU=xxxxxx,DC=asia,DC=xxxxxx,DC=com";

//$dispname=$_POST['employeeID'];
$dispname="100676";


$filter ="(&(objectClass=user)(displayName=$dispname))";

$attr = array("sn","givenname","employeeid","distinguishedname","displayname","samaccountName","department","manager","mail","title","thumbnailphoto");

$result = ldap_search($ldap_connection,$base_dn,$filter,$attr);

$rescount = ldap_count_entries($ldap_connection,$result);

$data = ldap_get_entries($ldap_connection,$result);


// echo json_encode($data);

if ($data["count"] > 0)
{
for ($i=0; $i<$data["count"]; $i++)
{
echo "<p> sn: " . $data[$i]["sn"][0]."<br/>";
echo "givenname: ". $data[$i]["givenname"][0] ."<br/>" ;
echo "employeeID: " . $data[$i]["employeeid"][0]."<br/>";
echo "distinguishedName: " . $data[$i]["distinguishedname"][0]."<br/>";
echo "displayName: " . $data[$i]["displayname"][0]."<br/>";
echo "sAMAccountName: " . $data[$i]["samaccountname"][0]."<br/>";
echo "department: ". $data[$i]["department"][0]."<br/>";
echo "manager: " .$data[$i]["manager"][0]."<br/>";
echo "mail: ". $data[$i]["mail"][0]."<br/>";
echo "title: " .$data[$i]["title"][0]."<br/>";
echo "photo: " .$data[$i]["thumbnailphoto"][0]."<br/>";
echo "<br/><br/>";
}
}
else
        {
            echo "<p>No results found!</p>";
        }


?>
&#13;
&#13;
&#13;

我现在得到的输出类型:

&#13;
&#13;
<p> sn: xxxxxx<br/>givenname: xxxxx<br/>
   employeeID: 0050<br/
   >distinguishedName: CN=xxxx     xxxxx,OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxxxxx,DC=com<br/>
   displayName: Mark Hewettk<br/>sAMAccountName: xxxxxxx<br/>
   department: xxxxx<br/>manager: CN=xxxxxx xxxxxxx,OU=Employees,OU=Accounts,OU=India,DC=asia,DC=xxxx,DC=com
   <br/>
   mail: mhewettk@abc.com<br/>
   title: xyz<br/>
   photo :����%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������
&#13;
&#13;
&#13;

我不想回应数据(我这样做是为了向大家展示它正在获取正确的数据),我想把它作为JSON,以便我可以在我的UI中使用它。

请帮助我了解如何从Active Directory中获取数据作为JSON?

JS参与:

&#13;
&#13;
$('.leaderboard li').on('click', function () {
 $.ajax({
 url: "../popupData/activedirectory.php", // your script above a little adjusted
 type: "POST",
 data: {id:$(this).find('.parent-div').data('id')},
 success: function(data){
 console.info(data);
 data = JSON.parse(data);
 $('#popup').fadeIn();


//whatever attributes you want to pull from active directory

 error: function(){
 alert('failed, possible script does not exist');
 }
 });
 });
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如何使用json_encode将从ldap_get_entries返回的数组转换为json对象

echo json_encode($data, JSON_PRETTY_PRINT);