0

I am relatively new to PHP development and would appreciate some help pulling in an array of customers accessed from the Shopify REST API (to a plugin I am writing). This is more about how to handle what is returned than the API itself.

My code is below. I have tried quite a few iterations and this is just the latest failure. My goal is pretty simple, namely retrieve a list of customers.

The REST API call is working i.e. if I copy target_URL into the browser I receive back customers just fine...

{"customers":[{"id":<ID>,"email":"<eMail>"}]}

which is exactly what I'd expect. So, all seems to be well in terms of the call itself (credentials, endpoint, parameters, etc).

However, the logged entry shows the following (see the code below):

Customers Returned = Array,,

How do I decode this so I can iterate through as an array?

  $args = array(
    'headers' => array('Content-Type' => 'application/json', 'Accept' => '*/*'),
  );
  $response = wp_remote_get( $target_url , $args );  
  $customer_array = json_decode( $response['body'], true );
  error_log('Customers Returned  = ' . $customer_array['customers'] . "," . $customer_array['id'] . "," . $customer_array['email']);

closed as off-topic by Michael, Nathan Johnson, Krzysiek Dróżdż, Jacob Peattie, Howdy_McGee May 12 '18 at 19:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

0

Follow your json template. The result of json_decode will be like this

array (
  'customers' => 
  array (
    0 => 
    array (
      'id' => 1123,
      'email' => '<eMail>',
    ),
  ),
)

So, you should use foreach loop, or you can get the value with specify key.

eg:

$customer_array['customers'][0]['id']
$customer_array['customers'][0]['email']

Not the answer you're looking for? Browse other questions tagged or ask your own question.