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

palla451's avatar

Laravel passport error GuzzleHttp curl 6

Hi all, i am creating a layer of bees with laravel passport. I installed laravel on homestead and i setting the hosts:

hosts


192.168.10.10 laravel_api.local

api.web


Route::post('login', 'UserController@login');
Route::post('register', 'UserController@register');


UserController


   public $successStatus = 200;

    public function login(Request $request)
    {
        if(Auth::attempt(['email' => $request->email, 'password' => $request->password]))
        {
            $oClient = OClient::where('password_client', 1)->first();
            return $this->getTokenAndRefreshToken($oClient, $request->email, $request->password);
        }else {
            return response()->json(['error' => 'Unauthorised'], 401);
        }
    }

    public function register(Request $request) {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required',
            'c_password' => 'required|same:password',
        ]);

        if ($validator->fails()) {
            return response()->json(['error'=>$validator->errors()], 401);
        }

        $password = $request->password;
        $input = $request->all();
        $input['password'] = bcrypt($input['password']);
        $user = User::create($input);
        $oClient = OClient::where('password_client', 1)->first();
        return $this->getTokenAndRefreshToken($oClient, $user->email, $password);
    }

    private function getTokenAndRefreshToken($oClient, $email, $password)
    {
        $http = new Client();
        $response = $http->request('POST', 'http://laravel_api.local/oauth/token', [
            'form_params' => [
                        'grant_type' => 'password',
                        'client_id' => $oClient->id,
                        'client_secret' => $oClient->secret,
                        'username' => $email,
                        'password' => $password,
                        'scope' => '*',
            ],
        ]);

        $result = json_decode((string) $response->getBody(), true);
        return response()->json($result, $this->successStatus);
    }


When i register a new user and test with PostMan i have this messagge

GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: laravel_api.local (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in file /home/vagrant/code/laravel_api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 200

how can I solve this problem?

0 likes
1 reply
frankincredible's avatar

If you're using vagrant (which I am assuming based on the file path in the error message you posted) to serve your application, then your local PC's hosts file will not be seen from inside your Laravel Application. The vagrant box will have it's own hosts file that you'd have to ssh into. You'd be better off setting IP address in your .env as something like MY_API_HOST=192.168.10.10 (and a MY_API_PORT key if it you want)

That said why would you trying to hit 'http://laravel_api.local/oauth/token' via Guzzle inside of your application? If you need to hit an internal endpoint from within your application, that's usually a sign that at least some portion of the logic in that Controller method needs to be extracted out into a Service, a Helper, a Model, etc.... and then called directly from within the controller that currently needs it... not by making a new post to an endpoint within your application.

For example, you could perhaps extract this logic into a public function refreshToken() on the User model, and in both of the controllers, call $user->refreshToken();

Please or to participate in this conversation.