从Azure AD获取用户 [英] Fetch users from Azure AD
问题描述
我不知道为什么我的循环根本不起作用.我已经成功连接到我的客户目录,并且能够获取一些用户.我遵循了 PHP说明 .但是本教程不包含仅获取默认用户100个页面大小的所有用户的示例.
I can't figure out why my loop isn't working at all. I have successfully connected to my clients directory and I am able to fetch some users. I have followed the PHP instructions. But this tutorial doesn't include example for fetching all users only the default page size of 100 users.
I am aware of the skipToken (explained here) but for some reason I am not been able to get it work with my loop.
基本上,我先定义一个数组和两个子数组.
Basically first I define an array, and two sub arrays.
$myArray = array();
$myArray['skipToken'] = "";
$myArray['users'] = "";
然后,我将执行第一次访存,这样我就可以获得skipToken和随之而来的一堆用户.
Then I'll perform the first fetch so I can get skipToken and bunch of users that come along.
require_once("GraphServiceAccessHelper.php");
$users = GraphServiceAccessHelper::getFeed('users');
将值推入已经存在的数组中.
Pushing values into already existing arrays.
$myArray['skipToken'] = $users->{'odata.nextLink'};
$myArray['users'][] = $users->{'value'};
现在它们充满了信息.现在该循环了!
Now they are filled with information. Now its time to loop!
for($i = 0; $i < 2; $i++){
if($myArray['skipToken'] != ""){
$skipToken = $myArray['skipToken'];
require_once("GraphServiceAccessHelper.php");
$users = GraphServiceAccessHelper::getNextFeed('users', $skipToken);
$myArray['skipToken'] = $users->{'odata.nextLink'};
$myArray['users'][] = $users->{'value'};
}
}
控制台因错误而启动,它指向循环skipToken定义部分:
Console fires up from error, that points to loop skipToken defining part:
Notice: Undefined property: stdClass::$odata.nextLink
$myArray['skipToken'] = $users->{'odata.nextLink'};
推荐答案
好的,我知道了.
首先,我必须删除所有实际令牌之前的所有内容.
First I had to remove everything before actual token.
$skipToken = $users->{'odata.nextLink'};
$skipToken = substr($skipToken, strpos($skipToken, "=") + 1);
然后在循环内使用,获取新的 skipToken 并执行与上述相同的操作:
Then inside the loop use that get new skipToken and do the same like above:
$new = GraphServiceAccessHelper::getNextFeed('users', $skipToken);
if(isset($new->{'odata.nextLink'})){
$skipToken = empty($new->{'odata.nextLink'});
} else{
break;
}
$skipToken = substr($skipToken, strpos($skipToken, "=") + 1);
$myArray['tokens'] = $skipToken;
$myArray['users'][] = $new->{'value'};
通过检查'odata.nextLink"
是否存在,由于最后一页不包含'odata.nextLink'
,因此我可以轻松地停止while循环.
By checking if 'odata.nextLink"
exists I can easily stop the while loop since lastpage doesn't contain 'odata.nextLink'
.
if(isset($new->{'odata.nextLink'})){
$skipToken = empty($new->{'odata.nextLink'});
} else{
break;
}
我正在将每个100个用户数组附加到另一个数组,我可以调用该数组在PHP之外轻松地使用它.
I am appending each 100 user array to another array that I can call easily use it outside PHP.
这篇关于从Azure AD获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!