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...


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']);

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.



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