0

I'm making a GET request to shopify API, and it gives back a response in json. I'm then using pHp to take the response and transform it into arrays put into a csv file. But I'm having a error.

The php code:

<?php
$baseUrl = 'https://myapikey@mysite.myshopify.com/admin/';

$response = json_decode(file_get_contents($baseUrl.'orders.json?limit=250')); 
$fp = fopen('file.csv', 'w');
var_dump($response->orders[0]->line_items[0]->properties);
foreach ($response->orders as $order_index => $order) {
   foreach ($order->line_items as $item_index => $item) {
     echo "order: {$order_index} item: {$item_index}";
     var_dump($item->properties);  
 fputcsv($fp, $item);
   }
} 
fclose($fp);
?>
This is giving me an error:
 Warning: fputcsv() expects parameter 2 to be array, object given in C:\wamp\www\test\index.php on line 57 

where line 57 is : fputcsv($fp, $item);

Can you please help me how to make it an array and insert into csv?

@Squeegy, thanks for your answer. It is working so far, the only problem is that in the csv its missing some important values. In the echo they show great and here its a part of the first loop:

 

order: 0 item: 0

array (size=9)
  0 => 
    object(stdClass)[6]
      public 'name' => string 'Glove Size' (length=10)
      public 'value' => string 'L' (length=1)
  1 => 
    object(stdClass)[7]
      public 'name' => string 'Hat Size' (length=8)
      public 'value' => string 'S/M' (length=3)
  2 => 
    object(stdClass)[8]
      public 'name' => string 'Pant Size' (length=9)
      public 'value' => string '32x34' (length=5)
  3 => 
    object(stdClass)[9]
      public 'name' => string 'Right or Left Handed?' (length=21)
      public 'value' => string 'Right' (length=5)
  4 => 
    object(stdClass)[10]
      public 'name' => string 'Shirt Size' (length=10)
      public 'value' => string 'S' (length=1)
  5 => 
    object(stdClass)[11]
      public 'name' => string 'Shoe Size' (length=9)
      public 'value' => string '10.5' (length=4)
  6 => 
    object(stdClass)[12]
      public 'name' => string 'shipping_interval_frequency' (length=27)
      public 'value' => string '1' (length=1)
  7 => 
    object(stdClass)[13]
      public 'name' => string 'shipping_interval_unit_type' (length=27)
      public 'value' => string 'Months' (length=6)
  8 => 
    object(stdClass)[14]
      public 'name' => string 'subscription_id' (length=15)
      public 'value' => string '1522' (length=4)
 

But in the csv None of the values I wrote above is included. Any idea why?

  • Is this what it looks like when you do `(array) $item`? If that's the case you'll need to go one level deeper in the loop still. Because you have an array now, but the value of each key is still an object. And unless that object has an `__toString()` function, it'll just result in NULL. So that may be why your CSV is still empty. – Squeegy May 12 '15 at 14:54
  • The csv is not empty, but doesnt have the echo values –  May 12 '15 at 14:56
  • Its in the csv file, something like: manual, 1046740548, 455951420, 1, 1, 1, Athletic Style Auto renew (ships every 1 Months), 1292559264, Athletic Style Auto renew (ships every 1 Months), Array, 1, 1, 0, Array –  May 12 '15 at 15:03
  • The shopify API after the GET request gives back a long response in json with many values, but after I dump most of that in the end I get the echo with the first loop that I wrote in the question. I just want the echo response to be in the csv. var_dump($item->properties); //this is the part that dumps all unnecessary data – –  May 12 '15 at 15:05
  • Yup, that sounds about right. `fputcsv()` doesn't work recursively. So any array value that's not a string, it just casts to a string `(string) $value` and puts that as the result. You'll need to do some further work organizing the structure into something you want, because ultimately each time you call `fputcsv()` you need to be passing it a simple array like `array('1234', 'foo', 'bar')` and nothing else. And without knowing the exact structure of your JSON, I can't help you much beyond just suggesting to step back and plan out what the end result you're looking for is. – Squeegy May 12 '15 at 15:09
  • @Squeegy Yes I did another loop and those are showing now. You are a great person. Thanks a lot. ps. I added a third foreach: foreach ($response->orders as $order_index => $order) { foreach ($order->line_items as $item_index => $item) { foreach ($item->properties as $item_properties => $properties) { echo "order: {$order_index} item: {$item_index}"; var_dump($item->properties); $array = (array) $properties; fputcsv($fp, $array); } } } fclose($fp); –  May 12 '15 at 15:22
  • Awesome! I'm glad we could work this out. I've done too many XML/JSON to CSV crap to know it can be a pain some times. – Squeegy May 12 '15 at 15:25

1 Answers1

1
$array = (array) $item;

fputcsv($fp, $array);

Found here Convert PHP object to associative array

Community
  • 1
  • 1
Squeegy
  • 869
  • 9
  • 20