Laravel consuming API and transforming object to array

Asked

Viewed 729 times

1

Next I’m making a requisition like get and is returning me one objeto and would like this to be an array , have tried using

$dados= Array.of($response->getBody()->getContents());

but of error! error

Parse error: syntax error, unexpected '.', expecting '('

MY CODES ->

web api.

use GuzzleHttp\Client;


Route::get('/', function () {


$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://teste.herokuapp.com',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);
    $response = $client->request('GET', 'api/login');
    $dados= $response->getBody()->getContents();

    dd($dados);

   return view('posts.index', compact('dados'));
});

dd($given)

"{"contas":[{"id":7,"email":"[email protected]","senha":"4154512"},{"id":1,"email":"[email protected]","senha":"lucas123"},{"id":14,"email":"[email protected] "
  • Have you tried this https://stackoverflow.com/questions/4345554/convert-php-object-associative-array ?

1 answer

0

Try it like this

$dados= json_decode($response->getBody()->getContents())->contas;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.