Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : LDAP Functions : ldap_get_entries

ldap_get_entries

Get all result entries (PHP 4, PHP 5)
array ldap_get_entries ( resource link_identifier, resource result_identifier )


Code Examples / Notes » ldap_get_entries

markus dot schabel

When you like to get the entry from LDAP in the same style as ldap_add(), then you can use the following function to convert this entry.
<?php
/**
* Take an LDAP and make an associative array from it.
*
* This function takes an LDAP entry in the ldap_get_entries() style and
* converts it to an associative array like ldap_add() needs.
*
* @param array $entry is the entry that should be converted.
*
* @return array is the converted entry.
*/
function cleanUpEntry( $entry ) {
$retEntry = array();
for ( $i = 0; $i < $entry['count']; $i++ ) {
$attribute = $entry[$i];
if ( $entry[$attribute]['count'] == 1 ) {
$retEntry[$attribute] = $entry[$attribute][0];
} else {
for ( $j = 0; $j < $entry[$attribute]['count']; $j++ ) {
$retEntry[$attribute][] = $entry[$attribute][$j];
}
}
}
return $retEntry;
}
?>


asohn

Some code I put together. Maybe yall can benefit from it.
<?php
function search_results($info) {
 foreach ($info as $inf) {
   if (is_array($inf)) {
     foreach ($inf as $key => $in) {
       if ((count($inf[$key]) - 1) > 0) {
         if (is_array($in)) {
           unset($inf[$key]["count"]);
         }
         $results[$key] = $inf[$key];
       }
     }
   }
 }
 $results["dn"] = explode(',', $info[0]["dn"]);
 return $results;
}
$user = "asohn";
$ds = ldap_connect("ldap://DOMAIN.net");
if ($ds) {
 $r = ldap_bind($ds);
 $sr = ldap_search($ds, "ou=customers,dc=DOMAIN,dc=net", "uid=".$user);
 $info = ldap_get_entries($ds, $sr);
 
 echo $info["count"]." Search Result(s) for \"".$user."\"\n";
 
 $results = search_results($info);
 foreach ($results as $key => $result) {
   echo "  ".$key."\n";
   if (is_array($result)){
     foreach($result as $res){
       echo "    ".$res."\n";
     }
   }
 }
 ldap_close($ds);
}
?>


c dot green

In response to the first message ldap_get_entries, I think there is some confusion with the dynamic typing of php.
If the result is a string doing $foo[0] will return the first character of the string.
In the case of an array $foo[0] will return the entire first element.
Its not to do with the 'dn' in particular, rather the fact that the dn is a scalar value (ie a string) rather than an array, and the indexing works differently in either case.
For debugging purposes I would recommend using something like :
$value = is_array($foo) ? $foo[0] : $foo;


reaper

If you're dealing with Active Directory and need to get values like 'lastlogon', 'pwdlastset' or similar, you'll notice that AD gives the values as Windows FILETIME timestamps. That means, the values are 100-nanosecond units passed since 1.1.1600 00:00:00.
To convert these to unix timestamps which PHP's date functions understand, one easy way would be the following :
function win_filetime_to_timestamp ($filetime) {
 $win_secs = substr($filetime,0,strlen($filetime)-7); // divide by 10 000 000 to get seconds
 $unix_timestamp = ($win_secs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
 return $unix_timestamp;
}


helmut dot patay

If you loop over the entries, like
$entries = ldap_get_entries( $ds, $sr );
watch out!
you have to check with is_null the last entry
because you will get one entry more than the search found,
but the last one will be null
so you are safe if you do:
for ( $i = 0; $i < count( $entries ); $i++ ) {
   if ( is_null( $entries[ $i ] ) ) continue;
   ...
}


matt.s remove

I noticed the same thing Ron did, except I couldn't get php to return the attribute values using numerical pointers at all.  I ended up using an eval() to pass in the associative name:
$ds=ldap_connect("ldapserver");
if ($ds) {
   $r=ldap_bind($ds); #bind to directory
   $sr=ldap_search($ds,"dc=domain,dc=net", "uid=user"); #search for user
   
   print  "Number of entires returned is " .ldap_count_entries($ds,$sr)."

";
   $info = ldap_get_entries($ds, $sr); #get the result
   
   print "Data for ".$info["count"]." items returned:

";
   for ($i=0; $i<$info["count"]; $i++) {  #loop though ldap search result
     
     print "dn: " . $info[$i]["dn"] . "
"; #print dn
   
     for ($ii=0; $ii<$info[$i]["count"]; $ii++) { #loop though attributes in this dn
       print "&nbsp;&nbsp;" . $info[$i][$ii] . ": "; #print attribute name
       $attrib = $info[$i][$ii]; #set attribute
       eval("print \$info[\$i][\"$attrib\"][0];"); #print attribute value
       print "
";
     }
     
     print "
";
   }
   
   ldap_close($ds);
   
} else {
   echo "<h4>Unable to connect to LDAP server</h4>";
}


ovidiu geaboc
I have found that ldap_get_entries() function doesn't handle binary data correctly.  I had to write my own using ldap_get_values_len().
// will use ldap_get_values_len() instead and build the array
// note: it's similar with the array returned by
// ldap_get_entries() except it has no "count" elements
$i=0;
$entry = ldap_first_entry($this->conn, $this->srchRslt);
do {
    $attributes = ldap_get_attributes($this->conn, $entry);
    for($j=0; $j<$attributes['count']; $j++) {
       $values = ldap_get_values_len($this->conn, $entry,$attributes[$j]);
       $this->rawData[$i][$attributes[$j]] = $values;
    }      
    $i++;
   }
while ($entry = ldap_next_entry($this->conn, $entry));
//we're done
return ($this->rawData);


mackstann / mack%at%incise%dot%org

I find the ["count"] items in these arrays highly annoying, so I made a function to remove them recursively:
function rCountRemover($arr) {
 foreach($arr as $key=>$val) {
   # (int)0 == "count", so we need to use ===
   if($key === "count")
     unset($arr[$key]);
   elseif(is_array($val))
     $arr[$key] = rCountRemover($arr[$key]);
 }
 return $arr;
}


reuben dot helms

Helmuts programming example is incorrect.
PHP arrays start from zero, so your first entry is $entry[0] and your last entry is $entry[$entry["count"] - 1].
$entry[$entry["count"]] will never exist, thus his usage of is_null.
Helmuts usage of is_null is not elegant, its just poor understanding of arrays.
Save some confusion and remove Helmuts entry and this one.


jim granger
Another way of ignoring the last null entry would be to subtract one from the iteration count like this:
for($i = 0; $i < count($result_array) - 1; $i++)
{
    ...
}
Helmut's method is far more elegant on its own but what I do is combine the above with the null test that he suggested. It may seem like overkill, but better safe than sorry.


pmichaud

Actually, the fact that ldap_get_entries returns attribute names as lowercase is really annoying, because ldap_get_attributes apparently does not.  This is really annoying, especially when having arrays of attribute names and having to worry about which call was used to retrieve entries from LDAP.

Change Language


Follow Navioo On Twitter
ldap_8859_to_t61
ldap_add
ldap_bind
ldap_close
ldap_compare
ldap_connect
ldap_count_entries
ldap_delete
ldap_dn2ufn
ldap_err2str
ldap_errno
ldap_error
ldap_explode_dn
ldap_first_attribute
ldap_first_entry
ldap_first_reference
ldap_free_result
ldap_get_attributes
ldap_get_dn
ldap_get_entries
ldap_get_option
ldap_get_values_len
ldap_get_values
ldap_list
ldap_mod_add
ldap_mod_del
ldap_mod_replace
ldap_modify
ldap_next_attribute
ldap_next_entry
ldap_next_reference
ldap_parse_reference
ldap_parse_result
ldap_read
ldap_rename
ldap_sasl_bind
ldap_search
ldap_set_option
ldap_set_rebind_proc
ldap_sort
ldap_start_tls
ldap_t61_to_8859
ldap_unbind
eXTReMe Tracker