简体   繁体   English

与Azure AD Graph API PHP示例的分页集成

[英]Pagination integration with Azure AD Graph API PHP Sample

I have successfully connected to my clients directory and I am able to fetch users from azure Active directory but just not all of them. 我已经成功连接到我的客户目录,并且能够从azure Active目录中获取用户,但不是全部。 I have followed the following PHP instructions however this tutorial doesn't include example for fetching all users but only the default page size of 100 users. 我遵循了以下PHP指令,但是本教程不包括获取所有用户的示例,而仅包含100个用户的默认页面大小。

I am aware of the skipToken ( explained here ) but I am not able to figure out how this should be integrated within my loop. 我知道skipToken( 在这里解释 ),但是我无法弄清楚应该如何将其集成到我的循环中。 here below is the page content. 以下是页面内容。

 <?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> 

This is a very similar issue here but it didn't help much, perhaps I am doing something wrong. 是一个非常相似的问题,但并没有太大帮助,也许我做错了什么。

Could anyone please assist? 谁能帮忙吗?

Based on your code, I do a demo on my side. 根据您的代码,我将进行演示。 You could refer to the following code. 您可以参考以下代码。

Modify getFeed function in GraphServiceAccessHelper.php 修改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;
            }

Modify page content 修改页面内容

 <?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>');
                                                       } 
      ?>               

Test Result: 测试结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM