首页 > 解决方案 > 与 Azure AD Graph API PHP 示例的分页集成

问题描述

我已成功连接到我的客户目录,并且能够从 azure Active Directory 中获取用户,但不是全部。我遵循了以下PHP 说明,但是本教程不包含获取所有用户的示例,而仅包含 100 个用户的默认页面大小。

我知道skipToken(解释here),但我无法弄清楚它应该如何集成到我的循环中。下面是页面内容。

<?php
    //Include menu options applicable to all pages of the web site
    include("PhpSampleTemplate.php");
?>

<HTML>
    <head>
        <title>
            Administration Page For Users
        </title>
    </head>

    <BODY>
        <h1>
            Administration Page For Users
        </h1>  
        <a href="CreateUser.php"><b>Create And Add A New User</b></a>    
        <br/><br/>
        <table border="1">
            <tr>
            <th>Display Name</th>
            <th>User Principal Name</th>
            <th>Object ID</th>
            <th>Account Enabled</th>        
            <th>Edit Link</th>
            <th>Delete Link</th>
            </tr>
            <?php
				$users = GraphServiceAccessHelper::getFeed('users');
                foreach ($users as $user){
						if ($user->{'accountEnabled'} == 1)
						  {
							  $accountEnabled = 'True';
						  }
						  else
						  {
							  $accountEnabled = 'False';
						  }
						$editLinkValue = "EditUser.php?id=".$user->objectId;
						$deleteLinkValue = "DeleteUser.php?id=".$user->objectId;
						echo('<tr><td>'. $user->{'displayName'}. '</td><td>'. $user->{'userPrincipalName'} .'</td>');
						echo('<td>'. $user->{'objectId'}.'</td>');
						echo ('<td>'. $accountEnabled.'</td>'); 
						echo('<td>' .'<a href=\''.$editLinkValue.'\'>'. 'Edit User' . '</a></td><td>'
							 .'<a href=\''.$deleteLinkValue.'\'>'. 'Delete User' . '</a></td></tr>');
				}
 ?>				

        </table>
    </BODY>
</HTML>

是一个非常相似的问题,但没有太大帮助,也许我做错了什么。

有人可以帮忙吗?

标签: phpazure-active-directory

解决方案


根据你的代码,我做一个演示。你可以参考下面的代码。

修改 GraphServiceAccessHelper.php 中的 getFeed 函数

 public static function getFeed($feedName){
                // initiaze curl which is used to make the http request.
                $ch = curl_init();
                // Add authorization and other headers. Also set some common settings.
                self::AddRequiredHeadersAndSettings($ch);
                // set url 
                $feedURL = "https://graph.windows.net/".Settings::$appTenantDomainName."/".$feedName;
                $feedURL = $feedURL."?".Settings::$apiVersion;
                curl_setopt($ch, CURLOPT_URL, $feedURL);
                //Enable fiddler to capture request
                //curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
                // $output contains the output string
                $output = curl_exec($ch);                               
                // close curl resource to free up system resources 
                //curl_close($ch);                                                                               
                while(true){
                $jsonOutput = json_decode($output);
                $users = $jsonOutput->{'value'};
                foreach ($users as $user){
                          $userArr[] = $user;
                         }
                 if(isset($jsonOutput->{'odata.nextLink'})){
                                                            $nextLink = $jsonOutput->{'odata.nextLink'};
                                                            $feedNextURL = "https://graph.windows.net/".Settings::$appTenantDomainName."/".$nextLink."&".Settings::$apiVersion;
                                                            curl_setopt($ch, CURLOPT_URL, $feedNextURL);
                                                            $output = curl_exec($ch);
                                                            }
                                                            else{
                                                                   curl_close($ch); 
                                                                    break;
                                                            };
                                                    };
                // There is a field for odata metadata that we ignore and just consume the value
                return $userArr;
            }

修改页面内容

 <?php
     $users = GraphServiceAccessHelper::getFeed('users');
     $total = count($users);
     $limit = 50;
     $pages = ceil($total / $limit);
     $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
                                                           'options' => array(
                                                                    'default'   => 1,
                                                                    'min_range' => 1,
                                                            ),
                                                )));
     $offset = ($page - 1)  * $limit;
     $start = $offset + 1;
     $end = min(($offset + $limit), $total);
     // The "back" link
     $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

      // The "forward" link
      $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

      // Display the paging information
      echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

      $subUsers = array_slice($users, $offset, $limit);
      foreach ($subUsers as $user){
                                      if ($user->{'accountEnabled'} == 1)
                                                           {
                                                                 $accountEnabled = 'True';
                                                           }
                                                           else
                                                           {
                                                               $accountEnabled = 'False';
                                                           }
                                                           $editLinkValue = "EditUser.php?id=".$user->objectId;
                                                           $deleteLinkValue = "DeleteUser.php?id=".$user->objectId;
                                                           echo('<tr><td>'. $user->{'displayName'}. '</td><td>'. $user->{'userPrincipalName'} .'</td>');
                                                           echo('<td>'. $user->{'objectId'}.'</td>');
                                                           echo ('<td>'. $accountEnabled.'</td>'); 
                                                           echo('<td>' .'<a href=\''.$editLinkValue.'\'>'. 'Edit User' . '</a></td><td>'
                                                            .'<a href=\''.$deleteLinkValue.'\'>'. 'Delete User' . '</a></td></tr>');
                                                       } 
      ?>               

测试结果:

在此处输入图像描述


推荐阅读