samedi 28 avril 2018

Laravel 5.1 Guzzle - Undefined offset: 0

I need to access an API so I use guzzle6 and I write a function:

public function test()
    {

$client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]);

try {
$res = $client->post('https://example.com/api/v2/oauth/token?grant_type=client_credentials', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],

    'auth' => [
        'Username' => 'user_5639',
        'Password' => 'pass_asdhbas67yausihd7qaw8'
]
            ]);

$res = json_decode($res->getBody()->getContents(), true);
}

catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

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

    }

but I got error:

ErrorException in Client.php line 346: Undefined offset: 0

When I try at POSTMAN the same request everything is fine: enter image description here

How to solve my problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire