php - 与 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>
这是一个非常相似的问题,但没有太大帮助,也许我做错了什么。
有人可以帮忙吗?
解决方案
根据你的代码,我做一个演示。你可以参考下面的代码。
修改 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">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</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>');
}
?>
测试结果:
推荐阅读
- javascript - Telegram html5 游戏返回空白页
- android - 如何在构建并包含为“aar”的 android 应用程序和颤振模块之间进行通信?
- java - 与 Python 在完全相同的代码上相比,Java 超出了时间限制
- python - 根据python中的值溶解多边形
- sql - SQL 两位小数
- google-cloud-platform - 重新启动后无法将远程桌面连接到 Windows 服务器
- python - 在多线程 Python 函数上使用 FastAPI 流式传输视频
- reactjs - 如何将参数传递给子组件
- javascript - Node.js vercel/pkg express 'return 0 error' 和 fastify 错误。错误:编译阶段文件或文件夹未包含在可执行文件中
- sql-server - sqlpackage.exe 发布失败,没有错误