Git Product home page Git Product logo

Comments (5)

caitlinrussell avatar caitlinrussell commented on August 15, 2024 6

Since there is some confusion around how to page through collections in this SDK, I added a wiki page devoted to this instead of cramming it into the examples page. Do you think this helps?

from msgraph-sdk-php.

caitlinrussell avatar caitlinrussell commented on August 15, 2024 5

Have you tried using a CollectionRequest instead of a regular request object?

$userIterator = $graph->createCollectionRequest("GET", "/users")
			        ->setReturnType(Model\User::class)
			        ->setPageSize(100);
$users = $userIterator->getPage();

foreach ($users as $user){
	$userArray[] = $user->getDisplayName();
}

from msgraph-sdk-php.

milanlatinovic avatar milanlatinovic commented on August 15, 2024 4

@Cbales thank you for your quick answer, I really appreciate it.

I have tried CollectionRequest as you proposed and it seems like I'm getting a proper results now. (It is a very large collection so I'll have to examine its contents some more to make sure that all pages are properly included).

Related to your code sample, I changed it a bit to be able to create an unique array with all the results. After reading trough your class I found isEnd() function and combined it with getPage() to iterate trough everything.

Right now, that part of the code looks like this:

      $userIterator = $graph->createCollectionRequest("GET", "/users")
            ->setReturnType(Model\User::class)
            ->setPageSize(100);

       while (!$userIterator->isEnd()) {
            $users = array_merge($users,$userIterator->getPage());
        }

Maybe you want to add this part with while() iterator to your documentation as well, just to make it more clear that whole array (with all of the results) can be created easily.

@Daveismyname thanks for the idea! Yes, this seems like a very nice approach to handle pagination issue using just Guzzle. Since my work is not related to Microsoft API only, and I do plan to use Guzzle quite a bit, I will use this/your approach in my pure-Guzzle parts of the code. I especially like the part with Api class for the re-usability.

So, thank you both, I believe that this tread will be helpful to the other users of SDK as well, we can mark this issue as resolved now. :) If I have any more useful materials related to this subject I will add it in future.

from msgraph-sdk-php.

dcblogdev avatar dcblogdev commented on August 15, 2024 1

@milanlatinovic I went the guzzle route mainly due to not getting a response to my issue. I have today but I've already moved away from the SDK so how I'm getting the records is like this:

This is taken from a Laravel project (I've released a Laravel package for this https://github.com/daveismynamelaravel/MsGraph)

	public function index()
	{
		$limit = 25;
		$offset = 50;
		$skip = request('next', 0);

		$messageQueryParams = array (
		    //"\$select" => "subject,receivedDateTime,from",
		    "\$orderby" => "displayName",
		    "\$skip" => $skip,
		    "\$top" => $limit,
		    "\$count" => "true",
		);

		$contacts = Api::get('me/contacts?'.http_build_query($messageQueryParams));

		$previous = null;
		$next = null;
		$total = $contacts['@odata.count'];
		if (isset($contacts['@odata.nextLink'])) {
			$first = explode('$skip=', $contacts['@odata.nextLink']);
			$skip = explode('&', $first[1]);
			$previous = $skip[0]-$offset;
			$next = $skip[0];

			if ($previous < 0) {
				$previous = 0;
			}

			if ($next == $total) {
				$next = null;
			}
		}

		return view('office365::admin.contacts.index', compact('contacts', 'previous', 'next'));
	}

The Api::get is calling a class where I have guzzle setup for reusability:

class Api
{
    protected static $baseUrl = 'https://graph.microsoft.com/beta/';

    public static function __callstatic($function, $args) {

        $options = ['get', 'post', 'patch', 'delete'];
        $path = (isset($args[0])) ? $args[0] : null;
        $data = (isset($args[1])) ? $args[1] : null;

        if (in_array($function, $options)) {
            return self::guzzle($function, $path, $data);
        }
    }

    protected static function guzzle($type, $request, $data = []) {

        $client = new \GuzzleHttp\Client;
        $tokenCache = new Token();

        $response = $client->$type(self::$baseUrl.$request, [
            'headers' => [
                'Authorization' => $tokenCache->getAccessToken(),
                'content-type' => 'application/json',
            ],
            'body' => json_encode($data),
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }

You might not want to do this way and stick with the SDK but it's an option.

from msgraph-sdk-php.

jozeflambrecht avatar jozeflambrecht commented on August 15, 2024

@milanlatinovic Thank you for your code example.

I can only use

while (!$userIterator->isEnd()) { ... }

when I do not use ->setPageSize(100) in the query. When I use ->setPageSize(100), I only get the first page.

Thanks again for sharing!

from msgraph-sdk-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.