Steven Grant Steven Grant - 5 months ago 62
JSON Question

access data array in JSON with Laravel & Guzzle

I'm trying to retrieve data from an API that gives a response like so:



I'm trying to use Guzzle to import into my Laravel app

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://localhost/members.json');
$data = (string) $response->getBody();

and then a foreach to loop over each record and then add it to the database. Right now though I'm struggling to drill down into a record.

What am I missing?

EDIT: Here's the foreach

foreach ($data['data'] as $person) {
'name' => $person->first_name,


Your $data variable holds json. Lets first make it a nice array and then loop it through

$response = json_decode($data, true);

Now the loop itself:

foreach($response['data'] as $element) {
    $firstName = $element['first_name'];
    $lastName = $element['last_name'];
    $group = $element['group'];
    $email = $element['email'];
    //Now here you can send it to the modele and create your db row.