Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Javed71's avatar

How to get Authenicate user data from GuzzleHttp Request (API)

I want to access a authenticate user data from another application . for that I used Guzzlehttp to get the client data on API request. I used the following lines of codes but it redirect with login form instead of data. Can anyone suggest what's the Error here?

                      $client = new Client();
              $response = $client->get('http://test-api.com/getUser', [
                'auth' => [
                    '[email protected]',
                     '123456'
                     ]
                ]);
               dd($response->getBody()->getContents());
0 likes
3 replies
bigbossstudio's avatar

Hi @Javed71,

That depend how you build your api. For example you can use basic auth to authenticate the user :

$client = new GuzzleHttp\Client();
$credentials = base64_encode('[email protected]:password');
$response = $client->get('http://test-api.com/getUser', [
    'Authorization' => ['Basic '.$credentials]
]);

Or you can use a token and put this one on the header :

$client = new GuzzleHttp\Client();
$credentials = 'token';
$response = $client->get('http://test-api.com/getUser', [
    'X-AUTH-TOKEN' => $credentials
);

First of all check on your api midleweare to make sur you enable the auth.

Regards, Eric

Javed71's avatar

@bigbossstudio I followed the way you said But getting same reply means instead of getting the data i got the sign in page

              $credentials = '7xgmE6pEO7In3vE6TZ49bxkjxpYpnj7VywHfaNbM1nuKDPsYjUNRIXLOi6rj';
              $client = new Client();                   
              $response = $client->get('http://test-api.com/getUser', [
                'X-AUTH-TOKEN' => $credentials,
                'auth' => [
                    '[email protected]',
                     '123456'
                     ]
                ]);
               dd($response->getBody()->getContents()); 
bhattraideb's avatar

I want to authenticate the user whose credential is stored in using GuzzleHTTP GET method. I already created register API. http://restapitest.local/api/v1/user/register :

public function register(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:5'
        ]);

        $name = $request->input('name');
        $email = $request->input('email');
        $password = $request->input('password');

       $user = new User([
            'name'          => $name,
            'email'         => $email,
            'password'      => bcrypt($password)
        ]);

        if($user->save()){
            $user->signin = [
                'href' => 'api/v1/user/singin',
                'method' => 'POST',
                'params' => 'email, password'
            ];
            $response = [
                'msg' => 'User created',
                'user' => $user
            ];

            return response()->json($response, 201);
        }

        $response = [
            'msg' => 'Error occured'
        ];

        return response()->json($response, 404);
    }

What I tried is http://restapitest.local/api/v1/user/singin:

public function signin()
    {
        $client = new \GuzzleHttp\Client();

        $response = $client->request(
            'GET', /*instead of POST, you can use GET, PUT, DELETE, etc*/
            'http://restapitest.local/api/v1/user/singin',
            [
                'auth' => ['email','password'] /*if you don't need to use a password, just leave it null*/
            ]
        );

        return response()->json($response->getBody());
}

Please, can someone post with a good explanation?

Please or to participate in this conversation.