X
X
x8seven2014-10-15 12:35:22
PHP
x8seven, 2014-10-15 12:35:22

How to make a selection from LDAP using PHP?

About me:
I'll admit right away, I'm just starting to learn PHP, so don't kick me too hard, point out my mistakes if possible.
Essence:
In hell there is a division with contacts of all employees of the company, I was given the task of making a list of contacts out of this in the form of a web page. After reading and realizing that this can be done through php, which I know at least a little. I googled for a couple of days and came up with this:

<?php
//Параметры подключения
$LDAP = array(
'server' => '192.168.1.5',
'port' => '389',
'user' => '[email protected]',
'pass' => 'YjQ9785DTWqZm76gJ7hCa8V7$f8t5r44w89Vtfh$',
'dn' => 'OU=contacts,DC=mydomen,DC=local'
);
// Подключаемся к серверу
$adconn = ldap_connect($LDAP['server'], $LDAP['port'])
    or die('Не могу подключиться к LDAP-серверу');

// Устанавливаем версию протокола 
ldap_set_option($adconn, LDAP_OPT_PROTOCOL_VERSION, 3);

// Привязка
ldap_bind($adconn, $LDAP['user'], $LDAP['pass'])
or die('Не могу подсоединиться к LDAP-серверу');

// выборка
$filter = "(sn=*)";
$att = array( "ou", "sn", "givenname", "mail");

$result=ldap_search($adconn, $LDAP['dn'], $filter, $att);

$info = ldap_get_entries($adconn, $result);

print $info["count"]." entries returned<p>";
?>

Problem:
I can’t understand why nothing returns, I will be glad for any help in enlightening this issue.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2014-10-15
@x8seven

This code works for me (of course, the parameters are changed to my domain).
Try like this:

$result=ldap_search($adconn, $LDAP['dn'], $filter, $att);
if ($result) {
    $info = ldap_get_entries($adconn, $result);
    print $info["count"]." entries returned";
} else
    print "LDAP search error ".ldap_errno($adconn)." (".ldap_error($adconn).")";

X
x8seven, 2014-10-15
@x8seven

It also worked for me after adding at the end
Apparently too frequent access to ldap fails if you do not break the connection
Now the question arose in another way, how to get exactly the data that I need from the multidimensional $info array, as I understand it from this one
print_r($info);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question