0

I want to retrieve only the email in a JSON response using guzzle in Laravel from an external API. Here is what I have tried

//Get all customer 
$allcus = 'https://api.paystack.co/customer';
$client = new Client();
$response = $client->request('GET', $allcus, [
  'headers' => [
    'Authorization' => 'Bearer '.'sk_live_#########################',
  ],
]); 

$cus_data = json_decode($response->getBody()->getContents()); 
//returns a json response of all customers
//dd($cus_data);

$cus_data_email = $cus_data->data->email;
dd($cus_data_email);

Using this returns error

$cus_data_email = $cus_data->data->email;

"message": "Trying to get property 'email' of non-object"

But when I tried this, it returns the customer in the first array

$cus_data_email = $cus_data->data[0]->email;

I don't want to return just one customer email. I want to retrieve all of the customers' emails.


This is the way the JSON response is

{
  "status": true,
  "message": "Customers retrieved",
  "data": [
    {
      "integration": ######,
      "first_name": null,
      "last_name": null,
      "email": "a###$gmail.com",
      "phone": null,
      "metadata": null,
      "domain": "live",
      "customer_code": "CUS_##########",
      "risk_action": "default",
      "id": #######,
      "createdAt": "2020-05-26T00:50:12.000Z",
      "updatedAt": "2020-05-26T00:50:12.000Z"
    },
    ...
0

1 Answer 1

1

What you're looking for is a loop!

$cus_data->data is an array, which is a variable that can store multiple values at once. These can be accessed with an index, and are commonly seen with loops.

I highly suggest reading the two links I supplied, the loop I'll be using is a foreach loop, as it's the most readable in this context. All the loops have their place, so it would pay to get familiar with them.

$emailsArray = []; // initialise an array
$emailsString = ""; // initialise a string

// Here's our loop, which will go over all the values of $cus_data->data
foreach($cus_data->data as $datum) {

    // $datum is the single value in $cus_data->data which we're currently looking at
    // Each of these values have an email property, which we access with arrow notation

    array_push($emailsArray, $datum->email); // add the email to our array
    $emailsString = $emailsString . $datum->email . ", "; // add the email to our string

}

Following this, $emailsArray will be an array (like we learned about above!) with all the emails from $cus_data->data.

$emailsString will contain the same information, just in a comma-separated string.

One thing to watch out for is if some of your data doesn't have emails! Then the code above could fail.

Admittedly, this isn't the shortest solution. For a problem like this, I would probably use array_map. The code here does the same thing in a more verbose format so we can understand it better.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.