-1

I am trying to retrieve information on users using the Microsoft Graph Library for PHP.

But this code runs into the error Trying to get property 'getGivenName' of non-object.

$user = $graph->createRequest("GET", "/users")
              ->setReturnType(Model\User::class)
              ->execute();

This is due to the fact, that $user is an array instead of an object!? What is wrong with the SDK (or my code)?

Is there any better documentation for the SDK???

Community
  • 1
  • 1
Odido
  • 13
  • 7

1 Answers1

2

This is due to the fact, that $user is an array instead of an object!?

That's right, since the endpoint GET Users returns the list of users, in the provided example:

 $users = $graph->createRequest("GET", "/users")
        ->setReturnType(\Microsoft\Graph\Model\User::class)
        ->execute();

$users contains an array of objects of Microsoft\Graph\Model\User type, and

 $givenName = $users[0]->getGivenName();  

gives GivenName property of first item in array.

A specific user could be requested via GET /users/{id | userPrincipalName} endpoint:

   $user = $graph->createRequest("GET", "/users/{$userId}")
        ->setReturnType(\Microsoft\Graph\Model\User::class)
        ->execute();

In that case $user object is of Microsoft\Graph\Model\User type:

   $givenName = $user->getGivenName();

Update

setReturnType function accept Microsoft Graph API entity type name, in your example it appears Model\User points to type which doesn't belong to Microsoft\Graph\Model namespace and as a result JSON response is not getting deserialized into class instance.

Instead of

setReturnType(Model\User::class)

try to specify fully qualified class name:

setReturnType(\Microsoft\Graph\Model\User::class)
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • I've already tried this: ``` $user = $graph->createRequest("GET", "/users/owagner@ad-libitum.info") ->setReturnType(Model\User::class) ->execute(); ``` This does not deliver an arry but an object - as intended. But I get the error `Undefined property: Microsoft\Graph\Model\User::$getGivenName` when executing `$givenName = $user->getGivenName();`. – Odido Mar 26 '19 at 16:07
  • sorry, still the same error: `Undefined property: Microsoft\Graph\Model\User::$getGivenName` – Odido Mar 27 '19 at 07:07
  • By the way: the debugger says, my `$user={Microsoft\Graph\Model\user}` and in the user.php there is `public function getGivenName()`. – Odido Mar 27 '19 at 12:01
  • i got the same error, Undefined property: Microsoft\Graph\Model\Message::$contentType – Syamlal Dec 16 '19 at 12:35