2

I am using the the Laravel Http Client to get a collection of users from the Microsoft Graph API. My code as follows is:

public function index()
    {
        $response = Http::withToken($this->accessToken)
                        ->get('https://graph.microsoft.com/v1.0/users');
        foreach($response['value'] as $user)
        {
            echo $user['displayName'] . '<br/>';
        }
    }

However, I want to be able to access user details as properties such as:

public function index()
    {
        $response = Http::withToken($this->accessToken)
                        ->get('https://graph.microsoft.com/v1.0/users');
        foreach($response->value as $user)
        {
            echo $user->displayName . '<br/>';
        }
    }

How would I go about this?

UPDATE

public function index()
    {
        $response = Http::withToken($this->accessToken)
                        ->get('https://graph.microsoft.com/v1.0/users')
                        ->throw();
        $users = $response->object();
        foreach($users->value as $user)
        {
            echo $user->displayName . '<br/>';
        }
    }
1

1 Answer 1

1

Try Below Code it's working properly.

public function index()
{
    $response = Http::withToken($this->accessToken)
                    ->get('https://graph.microsoft.com/v1.0/users');            
    $response = $response->object();
    foreach($response->value as $user)
    {
        echo $user->displayName . '<br/>';
    }
}

OR

public function index()
{
    $response = Http::withToken($this->accessToken)
                    ->get('https://graph.microsoft.com/v1.0/users');
    $response = json_decode(json_encode($response->json()));
    foreach($response->value as $user)
    {
        echo $user->displayName . '<br/>';
    }
}

OR

public function index()
{
    $response = Http::withToken($this->accessToken)
                    ->get('https://graph.microsoft.com/v1.0/users');
    $response = (object) $response->json();
    foreach($response->value as $user)
    {
        echo $user->displayName . '<br/>';
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Undefined property: Illuminate\Http\Client\Response::$value
I have to edit the answer please try this code. In my case, this code works properly.
what is going on with this: json_decode(json_encode($response->json()))? You call json() to make a json string, right? Then you call json_encode() on that already json-encoded string, then you call json_decode() on that double-json-encoded string?
In Laravel, response->json() often returns an array, so I converted it to a string with the json_encode function and then converted the same JSON string to an object with the json_decode function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.