1

I have an API /getuser which returns a reponse along with the user object:

$user = session()->get('current_user');
return response()->json(['user'=>$user]);

Now, I want to get the said returned api within my function public function show_user().

I tried $current_user = return redirect('/getuser'); but it's giving me an error - I don't know why specifically but I'm pretty sure you can't assign a returned response on a variable perhaps?

Reason why I'm using a different API on getting the user is because of Sessions. The api routes cannot read/store sessions properly (probably because they're stateless or something) so I placed that /getuser on the web routes which works perfectly. I could call the web "api" anywhere and it would give me the current user. Now however, I cannot reference the said current user within the api routes by doing session()->get()... since it wouldn't read sessions. My solution is to call that /getuser api I made on web routes and assign its returned response on a variable - which I have no idea how.

Is there anyway I could achieve something like that? Thanks a ton!

1 Answer 1

2

You can use guzzle in laravel when you need to call api in your controller.

use GuzzleHttp\Client;
class MyController extends Controller {

    public function show_user()
    {
        $client = new Client();
        $res = $client->request('GET', '/getuser', [
            'form_params' => [
                'KEY' => 'VALUE',
            ]
        ]);

        $result= $res->getBody();
        dd($result);

}

Read blog here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the suggestion. It gives me this error: cURL error 3: <url> malformed (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
Which points to this: ? new ConnectException($message, $easy->request, null, $ctx) : new RequestException($message, $easy->request, $easy->response, null, $ctx);
@devReb did you follow instruction in the blog post?
Thanks for this. I managed to solved it by using web routes. I will keep this method as well for future purposes if ever I need to use it. God bless! =)
@devReb glad it help

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.