How to get information about users in the directory with the Stanford Web Application Toolkit
From Web Services Wiki
Contents |
Problem
You'd like to look up a Stanford user's first name, last name, e-mail address, affiliation, or other information in the directory within your application.
Solution
The StanfordPerson class is a straightforward approach to searching for information about specific users in the directory.
Creating a new StanfordPerson
There are two primary ways to create a StanfordPerson depending on the nature of your application or script.
Getting information about a single user
When getting information about a single user, use the code below for optimal efficiency. It connects to the directory, fetches the data, and then disconnects.
// Include StanfordPerson include_once("stanford.person.php"); // Create a new StanfordPerson with the user's SUNetID $person = new StanfordPerson("my_sunetid"); // Echo the first name echo "<p>", $person->get_sunetid(), "'s first name is ", $person->get_first_name(), "</p>";
Getting information about multiple users
When getting information about multiple users, we strongly suggest using the following method, as it does not create a new connection to the directory on every query. Since we pass the same StanfordDirectory object to the StanfordPerson constructor each time, we use the same connection for all searches.
// Include StanfordPerson include_once("stanford.person.php"); // Create a new StanfordDirectory connection $directory = new StanfordDirectory(); // Create a list of users $sunetids = array("sunetid1", "sunetid2", "sunetid3", "sunetid4"); // Loop through users and create new StanfordPersons foreach($sunetids as $sunetid) { // Pass the SUNetID and directory object to the StanfordPerson constructor $person = new StanfordPerson($sunetid, $directory); // Echo the first name echo "<p>", $person->get_sunetid(), "'s first name is ", $person->get_first_name(), "</p>"; } // Close the connection $directory->disconnect();
Accessing the data
Use the functions shown in the example below to get information about a StanfordPerson.
// Output user info echo '<p><strong>User information:</strong></p>'; echo '<p>'; echo '<strong>First name:</strong> ' . $person->get_first_name() . '<br/>'; echo '<strong>Middle name:</strong> ' . $person->get_middle_name() . '<br/>'; echo '<strong>Last name:</strong> ' . $person->get_last_name() . '<br/>'; echo '<strong>E-mail address:</strong> ' . $person->get_email() . '<br/>'; echo '<strong>SUNetID:</strong> ' . $person->get_sunetid() . '<br/>'; echo '<strong>Primary affiliation:</strong> ' . $person->get_primary_affiliation() . '<br/>'; echo '<strong>Is a student?:</strong> ' . (($person->is_a_student()) ? "yes" : "no") . '<br/>'; echo '<strong>Is faculty?:</strong> ' . (($person->is_faculty()) ? "yes" : "no") . '<br/>'; echo '<strong>Is staff?:</strong> ' . (($person->is_staff()) ? "yes" : "no") . '<br/>'; echo '<strong>Is an affiliate?:</strong> ' . (($person->is_affiliate()) ? "yes" : "no") . '<br/>'; echo '</p>';
All available attributes
Below is a list of all attributes that are currently supported by the StanfordPerson class. Please note that depending on permissions and privacy settings, your application may not have access to much of the information that a user has entered on StanfordYou. See the discussion for more details.
// Basic information get_sunetid get_email // Name get_first_name get_last_name get_full_name get_middle_name // Contact information get_home_phone get_mobile_phone get_work_phone get_pager_number get_pager_email // Addresses get_home_postal_address get_work_postal_address get_permanent_postal_address // Other get_job_title // Status (true/false) is_a_student is_faculty is_staff is_affiliate
Discussion
Visibility restrictions in the directory
While using StanfordDirectory and StanfordPerson you will encounter users whose information is incomplete (e.g., a missing e-mail address). This may be because the information is simply not available or because it has been explicitly made private by the user or owner of the data. You may use StanfordYou to modify your own visibility within the directory.
Those who are developing applications that require access to private data must petition for more permissions. View the usage policy for more information.

