【问题标题】:Microsoft Graph API - paging large collectionsMicrosoft Graph API - 分页大型集合
【发布时间】:2019-06-19 15:41:28
【问题描述】:

我只是在查看 Microsoft Graph API PHP SDK 以获取大量资源,尤其是用户。

查看 SDK 文档,有两种获取用户的方法,一种使用 createRequest() 方法,另一种使用 createCollectionRequest() 方法。

文档建议使用createCollectionRequest(),然后只做一个while循环,array_mergegetPage()来创建一个数组。

while (!$docGrabber->isEnd()) {
  $docs = array_merge($docs,$docGrabber->getPage());
}  

问题是,我有大约 50,000 个用户的集合,所以这种方法不是特别有效。

我猜最大的问题是,我上面的例子(使用while循环)是为了避免使用API​​返回的@odata.nextLink

但是,如果我们真的想使用它,而不是返回单个数组中的每条记录呢?

谢谢

【问题讨论】:

    标签: php microsoft-graph-api microsoft-graph-sdks


    【解决方案1】:

    您可以通过以下方式访问下一个链接,而不是使用 getPage() 和该示例:

    $url = "/users";
    
    // Get the first page
    $response = $graph->createCollectionRequest("GET", $url)
                       ->setPageSize(50)
                       ->execute();
    
    if ($response->getNextLink())
    {
        $url = $response->getNextLink();
        // TODO: remove https://graph.microsoft.com/v1.0 part of nextlink
    } else {
    // There are no more pages.
        return null;
    }
    
    // get the next page, page size is already set in the next link
    $response = $graph->createCollectionRequest("GET", $url)
                      ->execute();
    

    【讨论】:

    • 感谢您回复我的问题。 $response 似乎是一个数组,因此调用 getNextLink() 会引发错误:Call to a member function getNextLink() on array。我正在使用 SDK 的 v1.9
    • 我的错误。我们需要删除 setReturnType 方法调用。更新了示例。 GraphResponse 返回后,再调用 getResponseAsObject()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    相关资源
    最近更新 更多