aleksov
696
2
General

Connect API with Laravel project

Posted 1 year ago by aleksov

I need to connect to an API with my Laravel 5.1 project so I write function to test:


public function testDng() {

    $client = new GuzzleHttp\Client(['verify' => false ]);
try {
    $res = $client->post('https://api.example.com/api/authenticate', [

    'form_params' => [
        'email' => '[email protected]',
        'password' => 'asdasd'
]
            ]);
return $res;
}
catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

    return response()->json(['data' => $result]);

    }

}

The result of this function is:


{"data":null}

but when I try this API in Postman app everything is fine and I get token:

https://i.stack.imgur.com/ZJaVM.png

So something is wrong with my function ... What caused the problem and why I cant get token?

Please sign in or create an account to participate in this conversation.