# PHP Guidance for user-metadata & user\_id

**URL:** <https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396>\
**Category:** Get Help\
**Tags:** auth0-php, php\
**Created:** [July 1, 2021, 1:31pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396 "2021-07-01T13:31:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![KFDEV](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kfdev/32/28903_2.png) [@KFDEV](https://community.auth0.com/u/KFDEV)\
**Post date:** [July 1, 2021, 1:31pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/1 "2021-07-01T13:31:03Z")

</div>

Hi all,

I’m struggling a huge amount trying to use the PHP API, the github sample is extremely basic ☹

So I can get a basic user back from the auth client after sign in

`$auth0->getUser()`

but this only has basic properties.  
How do I properly retrieve the user meta data?

This “solution” below seems like a bit of a hack

> [@How to get user\_metadata and app\_metadata after login, using PHP SDK?](https://community.auth0.com/t/how-to-get-user-metadata-and-app-metadata-after-login-using-php-sdk/50072):
>
> I have managed to setup a registration–\>login–\>logout flow using the PHP SDK. However I can’t managed to figure out on how to get a logged in users user\_metadata and app\_metadata. I can show some userinfo via $userInfo = $auth0-\>getUser(); this returns an array of the bare minimum user data like email, verified, last\_updated etc. But how do I get the additional data that is present on the raw json object if I inspect the user in the auth0 dashboard, mainly the user\_metadata and app\_metadata?

What am I actually meant to be using?  
I think I need to use the management API, but how do I get a users meta data from it?

`$mgmt_api = new \Auth0\SDK\API\Management( $access_token , $auth0_domain );`

While we are at it also:

How do I get a users ID from the signed in user?  
How do I set a users meta data?

If there are any more resources that would be great, I was kind of surprised that there is only 1 really basic PHP sample

Thanks

Karl

---

<div class="post-metadata">

**Author:** ![evansims](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/evansims/32/27011_2.png) [@evansims](https://community.auth0.com/u/evansims)\
**Post date:** [July 1, 2021, 4:03pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/2 "2021-07-01T16:03:01Z")

</div>

Hey Karl 👋

That solution is correct if you’re wanting to get metadata returned from the authentication responses, as it otherwise isn’t. Rules allow you to tailor the responses to meet your application’s needs for situations like this. Returning the metadata using a rule rather than making management API calls will save you extra network requests and API calls that will impact your rate limit quota.

When using the Management API, you should use an access token [suitable for Management API calls](https://auth0.com/docs/tokens/management-api-access-tokens). You can retrieve a user’s detail through the Management API using the `Auth0\SDK\API\Management\Users` class’ `get()` method.

Assuming you are using SDK 7.x:

```php
// 🧩 Your \Auth0\SDK\Auth0 configuration code here ...

try {
    $user = $auth0->getUser();
} catch (\Exception $e) {
   $user = null;
}

if ($user) {
    // This is the type of ID you were asking about, I believe? This is what you'd issue Management API user endpoint requests with.
    $userId = $user['sub'];

    // Instantiate the Management API class.
    $managementApi = new \Auth0\SDK\API\Management(AUTH0_MANAGEMENT_TOKEN, AUTH0_DOMAIN);

    // Fetch the user from the /users endpoint by the authenticated user's ID.
    $userResponse = $managementApi->users()->get($userId);

    if ($userResponse) {
        // Echo the Management API response, as it is:
        var_dump($userResponse);

        // Add or update a 'testing' property to the metadata with a random, unique ID.
        $userMetadata = $userResponse['user_metadata'] ?? [];
        $userMetadata['testing'] = uniqid();

        // Send a PATCH request to update the user.
        // Updatable properties can be referenced here https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id
        $managementApi->users()->update($userId, (object) ['user_metadata' => $userMetadata]);
    } else {
        die("User not found.");
    }
}

```

Or, with the new SDK 8.x that’s entering beta this week, the API is slightly updated:

```php
// 🧩 Your \Auth0\SDK\Auth0 configuration code here ...

use Auth0\SDK\Utility\HttpResponse;

$session = $auth0->getCredentials();

if ($session !== null) {
    // This is the type of ID you were asking about, I believe? This is what you'd issue Management API user endpoint requests with.
    $userId = $session->user['sub'];

    // Fetch the user from the /users endpoint by the authenticated user's ID.
    $apiResponse = $auth0->management()->users()->get($userId);

    // Will return false if the user was not found, or the network request failed.
    if (! HttpResponse::wasSuccessful($apiResponse)) {
        die("User not found.");
    }

    // Converts the PSR-7 HTTP Response from the network request into a PHP array representing the user.
    $userResponse = HttpResponse::decodeContent($apiResponse);

    // Echo the Management API response, as it is:
    var_dump($userResponse);

    // Add or update a 'testing' property to the metadata with a random, unique ID.
    $userMetadata = $userResponse['user_metadata'] ?? [];
    $userMetadata['testing'] = uniqid();

    // Send a PATCH request to update the user.
    // Updatable properties can be referenced here https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id
    $auth0->management()->users()->update($userId, ['user_metadata' => $userMetadata]);
}

```

(Writing this from my phone as I’m out at lunch at the moment, so apologies for any obvious code errors, but that is a general idea.)

Hope this answers your questions, and let us know if we can help further.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [July 2, 2021, 8:34am UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/3 "2021-07-02T08:34:37Z")

</div>

Thanks @evansims for helping on this one!

---

<div class="post-metadata">

**Author:** ![KFDEV](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kfdev/32/28903_2.png) [@KFDEV](https://community.auth0.com/u/KFDEV)\
**Post date:** [July 2, 2021, 9:44am UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/4 "2021-07-02T09:44:28Z")

</div>

Thank you very much, this is really useful 😃

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [July 2, 2021, 12:15pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/5 "2021-07-02T12:15:10Z")

</div>

No worries! We’re here for you!

---

<div class="post-metadata">

**Author:** ![edwardsmarkf](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/edwardsmarkf/32/16571_2.png) [@edwardsmarkf](https://community.auth0.com/u/edwardsmarkf)\
**Post date:** [July 5, 2021, 4:48pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/6 "2021-07-05T16:48:48Z")

</div>

FWIW, Auth0 tries to be very platform-independent, and so some examples may need a bit of help. when i was first using Auth0-PHP i struggled a great deal because a PHP example was incorrect/incomplete.

Hopefully that is fixed now, but i have not checked recently. here was my issue:

> [@Getting picture, nickname, etc in PHP](https://community.auth0.com/t/getting-picture-nickname-etc-in-php/32969/3):
>
> Please @edwardsmarkf let us know if that solved the issue!

i also had to HIRE a consultant to solve a couple of very basic PHP issues:

> [@Php prefill an email address](https://community.auth0.com/t/php-prefill-an-email-address/41070/10):
>
> hey dan - this link seemed to indicate there may be a better way to pre-populate the google login other than using login\_hint: Our best practice recommendations from Auth0 have been changed to discontinue using login\_hint as a way to pass this data, and instead recommend doing a namespaced parameter. e.g. `mycompany_showsignup=1` or `mycompany_initialscreen=signup` This will help avoid any conflicts with the appropriate use of those parameters. but that being said, i have run out of time…

i keep hoping the Auth0 will create user-driven cookbook-solution sections. these PHP issues are very common.

Konrad Sopala and Dan Woda both do excellent job supporting us, but of course they are spread too thin, as are most of the Auth0 staff. that is why i think a user driven cookbook might be the best approach.

---

<div class="post-metadata">

**Author:** ![evansims](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/evansims/32/27011_2.png) [@evansims](https://community.auth0.com/u/evansims)\
**Post date:** [July 5, 2021, 6:35pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/7 "2021-07-05T18:35:06Z")

</div>

Hey @edwardsmarkf 👋 Very sorry to hear about the issues you had; I only came only recently and wish I’d been able to help.

One of my goals with the new QuickStart for 8.0 was to introduce a cookbook-like, modular solution to the base QuickStart example developers can opt into to see how expanding on the basic configuration and login example works. I’ll iterate on this as time goes on. Hopefully this will help avoid others in the future hitting similar issues!

---

<div class="post-metadata">

**Author:** ![edwardsmarkf](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/edwardsmarkf/32/16571_2.png) [@edwardsmarkf](https://community.auth0.com/u/edwardsmarkf)\
**Post date:** [July 5, 2021, 6:45pm UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/8 "2021-07-05T18:45:20Z")

</div>

hey Evans -

believe me when i say i am NOT complaining, i am just totally thrilled to have such a fantastic login replacement. maintaining user login has been an ongoing headache for me. so much so that i just started setting user passwords equal to their email addresses!

complaining about the Auth0-PHP interface would be like complaining that Christie Brinkley has a tiny freckle on her toe.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [July 6, 2021, 10:54am UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/9 "2021-07-06T10:54:59Z")

</div>

😂😂😂😂 nice comparison!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 21, 2021, 10:55am UTC](https://community.auth0.com/t/php-guidance-for-user-metadata-user-id/65396/10 "2021-07-21T10:55:30Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
