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://[email protected]/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);
?>
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?
(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.fputcsv()doesn't work recursively. So any array value that's not a string, it just casts to a string(string) $valueand 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 callfputcsv()you need to be passing it a simple array likearray('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.