reldridge Posted January 11, 2016 Share Posted January 11, 2016 Noob alert, may need more help than most. I am very new to PHP and trying to learn as I test getting an Order export our of Ecwid shopping cart via their api to send my orders to our fulfillment house. Ecwid Shopping Cart API: http://developers.ecwid.com/api-documentation#orders I have been able to retrieve the json data and get a portion of it to export into csv. I am running into the issue with nested json objects just exporting "array" in the column when these are hit. This code snippet gives me half the export: <?php # An HTTP GET request example $url = 'https://app.ecwid.com/api/v3/{STOREID}/orders?paymentStatus=PAID&fulfillmentStatus=AWAITING_PROCESSING&token={TOKEN}'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); echo $data; $json_obj = json_decode($data, true); $fp = fopen('hardmerch_orders.csv', 'a'); foreach ($json_obj['items'] as $items) { fputcsv($fp, $items); } fclose($fp); ?> Updated snippet: I am using the below to try to build out the array for the nested object to export to csv but I am getting nothing in the spreadsheet file now. <?php # An HTTP GET request example $url = 'https://app.ecwid.com/api/v3/{STOREID}/orders?paymentStatus=PAID&fulfillmentStatus=AWAITING_PROCESSING&token={TOKEN}'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); // echo $data; $json_obj = json_decode($data, true); //Ecwid Shopping Cart //http://developers.ecwid.com/api-documentation#orders $output = array(); foreach ($json_obj['items'] as $item) { $output['total'] = $item['total']; $output['count'] = $item['count']; $output['offset'] = $item['offset']; $output['limit'] = $item['limit']; $output['items'] = $item['items']; $output['vendorOrderNumber'] = $item['items'][0]['vendorOrderNumber']; $output['subtotal'] = $item['items'][0]['subtotal']; $output['total'] = $item['items'][0]['total']; $output['email'] = $item['items'][0]['email']; $output['externalTransactionId'] = $item['items'][0]['externalTransactionId']; $output['paymentModule'] = $item['items'][0]['paymentModule']; $output['paymentMethod'] = $item['items'][0]['paymentMethod']; $output['tax'] = $item['items'][0]['tax']; $output['ipAddress'] = $item['items'][0]['ipAddress']; $output['couponDiscount'] = $item['items'][0]['couponDiscount']; $output['paymentStatus'] = $item['items'][0]['paymentStatus']; $output['paymentMessage'] = $item['items'][0]['paymentMessage']; $output['fulfillmentStatus'] = $item['items'][0]['fulfillmentStatus']; $output['orderNumber'] = $item['items'][0]['orderNumber']; $output['refererUrl'] = $item['items'][0]['refererUrl']; $output['orderComments'] = $item['items'][0]['orderComments']; $output['volumeDiscount'] = $item['items'][0]['volumeDiscount']; $output['customerId'] = $item['items'][0]['customerId']; $output['membershipBasedDiscount'] = $item['items'][0]['membershipBasedDiscount']; $output['totalAndMembershipBasedDiscount'] = $item['items'][0]['totalAndMembershipBasedDiscount']; $output['discount'] = $item['items'][0]['discount']; $output['usdTotal'] = $item['items'][0]['usdTotal']; $output['globalReferer'] = $item['items'][0]['globalReferer']; $output['createDate'] = $item['items'][0]['createDate']; $output['updateDate'] = $item['items'][0]['updateDate']; $output['createTimestamp'] = $item['items'][0]['createTimestamp']; $output['updateTimestamp'] = $item['items'][0]['updateTimestamp']; $output['customerGroupId'] = $item['items'][0]['customerGroupId']; $output['customerGroup'] = $item['items'][0]['customerGroup']; //['discountCoupon'] $output['name'] = $item['discountCoupon'][0]['name']; $output['code'] = $item['discountCoupon'][0]['code']; $output['discountType'] = $item['discountCoupon'][0]['discountType']; $output['status'] = $item['discountCoupon'][0]['status']; $output['discount'] = $item['discountCoupon'][0]['discount']; $output['launchDate'] = $item['discountCoupon'][0]['launchDate']; $output['usesLimit'] = $item['discountCoupon'][0]['usesLimit']; $output['repeatCustomerOnly'] = $item['discountCoupon'][0]['repeatCustomerOnly']; $output['creationDate'] = $item['discountCoupon'][0]['creationDate']; $output['orderCount'] = $item['discountCoupon'][0]['orderCount']; //['items'] $output['id'] = $item['items'][0]['id']; $output['productId'] = $item['items'][0]['productId']; $output['categoryId'] = $item['items'][0]['categoryId']; $output['price'] = $item['items'][0]['price']; $output['productPrice'] = $item['items'][0]['productPrice']; $output['weight'] = $item['items'][0]['weight']; $output['sku'] = $item['items'][0]['sku']; $output['quantity'] = $item['items'][0]['quantity']; $output['shortDescription'] = $item['items'][0]['shortDescription']; $output['tax'] = $item['items'][0]['tax']; $output['shipping'] = $item['items'][0]['shipping']; $output['quantityInStock'] = $item['items'][0]['quantityInStock']; $output['name'] = $item['items'][0]['name']; $output['tangible'] = $item['items'][0]['tangible']; $output['trackQuantity'] = $item['items'][0]['trackQuantity']; $output['fixedShippingRateOnly'] = $item['items'][0]['fixedShippingRateOnly']; $output['imageUrl'] = $item['items'][0]['imageUrl']; $output['fixedShippingRate'] = $item['items'][0]['fixedShippingRate']; $output['digital'] = $item['items'][0]['digital']; $output['productAvailable'] = $item['items'][0]['productAvailable']; $output['couponApplied'] = $item['items'][0]['couponApplied']; //['files'] $output['productFileId'] = $item['files'][0]['productFileId']; $output['maxDownloads'] = $item['files'][0]['maxDownloads']; $output['remainingDownloads'] = $item['files'][0]['remainingDownloads']; $output['expire'] = $item['files'][0]['expire']; $output['name'] = $item['files'][0]['name']; $output['description'] = $item['files'][0]['description']; $output['size'] = $item['files'][0]['size']; $output['adminUrl'] = $item['files'][0]['adminUrl']; $output['customerUrl'] = $item['files'][0]['customerUrl']; //['selectedOptions'] $output['name'] = $item['selectedOptions'][0]['name']; $output['value'] = $item['selectedOptions'][0]['value']; $output['valuesArray'] = $item['selectedOptions'][0]['valuesArray']; $output['Big'] = $item['selectedOptions'][0]['Big']; $output['type'] = $item['selectedOptions'][0]['type']; // THERE ARE MORE SELECTED OPTIONS BUT I AM LOST AT HOW THEY SHOULD BE ADDED TO THE ARRAY. //['taxes'] $output['name'] = $item['taxes'][0]['name']; $output['value'] = $item['taxes'][0]['value']; $output['total'] = $item['taxes'][0]['total']; $output['taxOnDiscountedSubtotal'] = $item['taxes'][0]['taxOnDiscountedSubtotal']; $output['taxOnShipping'] = $item['taxes'][0]['taxOnShipping']; //['billingPerson'] $output['name'] = $item['billingPerson'][0]['name']; $output['companyName'] = $item['billingPerson'][0]['companyName']; $output['street'] = $item['billingPerson'][0]['street']; $output['city'] = $item['billingPerson'][0]['city']; $output['countryCode'] = $item['billingPerson'][0]['countryCode']; $output['countryName'] = $item['billingPerson'][0]['countryName']; $output['postalCode'] = $item['billingPerson'][0]['postalCode']; $output['stateOrProvinceCode'] = $item['billingPerson'][0]['stateOrProvinceCode']; $output['stateOrProvinceName'] = $item['billingPerson'][0]['stateOrProvinceName']; $output['phone'] = $item['billingPerson'][0]['phone']; //['shippingPerson'] $output['name'] = $item['shippingPerson'][0]['name']; $output['companyName'] = $item['shippingPerson'][0]['companyName']; $output['street'] = $item['shippingPerson'][0]['street']; $output['city'] = $item['shippingPerson'][0]['city']; $output['countryCode'] = $item['shippingPerson'][0]['countryCode']; $output['countryName'] = $item['shippingPerson'][0]['countryName']; $output['postalCode'] = $item['shippingPerson'][0]['postalCode']; $output['stateOrProvinceCode'] = $item['shippingPerson'][0]['stateOrProvinceCode']; $output['stateOrProvinceName'] = $item['shippingPerson'][0]['stateOrProvinceName']; $output['phone'] = $item['shippingPerson'][0]['phone']; //['shippingOption'] $output['shippingMethodName'] = $item['shippingOption'][0]['shippingMethodName']; $output['shippingRate'] = $item['shippingOption'][0]['shippingRate']; $output['estimatedTransitTime'] = $item['shippingOption'][0]['estimatedTransitTime']; //['handlingFee'] $output['name'] = $item['handlingFee'][0]['name']; $output['value'] = $item['handlingFee'][0]['value']; $output['description'] = $item['handlingFee'][0]['description']; //['paymentParams'] $output['Company name'] = $item['paymentParams'][0]['Company name']; $output['Job position'] = $item['paymentParams'][0]['Job position']; $output['PO number'] = $item['paymentParams'][0]['PO number']; //$output['"Buyer's full name'] = $item['paymentParams'][0]['Buyer's full name']; //['discountInfo'] $output['value'] = $item['discountInfo'][0]['value']; $output['type'] = $item['discountInfo'][0]['type']; $output['base'] = $item['discountInfo'][0]['base']; $output['orderTotal'] = $item['discountInfo'][0]['orderTotal']; $output['hidden'] = $item['discountInfo'][0]['hidden']; } echo $output; $fp = fopen('hardmerch_orders.csv', 'a'); foreach ($output as $line) { fputcsv($fp, $items); } fclose($fp); ?> Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/ Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 Hi and Welcome to PHPFreaks. Could you paste up the results of var_dump($json_obj['items'][0]['items']); so we can see what's in there? Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529493 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) var_dump($json_obj['items'][0]['items']); Result: array(2) { [0]=> array(23) { ["id"]=> int(69696462) ["productId"]=> int(9673066) ["categoryId"]=> int(2455976) ["price"]=> int(22) ["productPrice"]=> int(22) ["sku"]=> string(5) "00006" ["quantity"]=> int(2) ["shortDescription"]=> string(122) "The original. Worn by Captain Marciano and his First Mate Jay from the very first season of Wicked Tuna , and from the ..." ["tax"]=> int(0) ["shipping"]=> float(12.53125) ["quantityInStock"]=> int(85) ["name"]=> string(13) "Black T-Shirt" ["isShippingRequired"]=> bool(true) ["weight"]=> int(2) ["trackQuantity"]=> bool(true) ["fixedShippingRateOnly"]=> bool(false) ["imageUrl"]=> string(64) "https://dpbfm6h358sh7.cloudfront.net/images/1039296/36454966.jpg" ["smallThumbnailUrl"]=> string(64) "https://dpbfm6h358sh7.cloudfront.net/images/1039296/36454968.jpg" ["fixedShippingRate"]=> int(0) ["digital"]=> bool(false) ["productAvailable"]=> bool(true) ["couponApplied"]=> bool(false) ["selectedOptions"]=> array(1) { [0]=> array(4) { ["name"]=> string(4) "Size" ["value"]=> string( "2X-Large" ["valuesArray"]=> array(1) { [0]=> string( "2X-Large" } ["type"]=> string(6) "CHOICE" } } } [1]=> array(22) { ["id"]=> int(69696463) ["productId"]=> int(15479536) ["categoryId"]=> int(2455976) ["price"]=> int(20) ["productPrice"]=> int(20) ["sku"]=> string(5) "00096" ["quantity"]=> int(2) ["shortDescription"]=> string(122) "Hard Merchandise hats are here! Fan favorite Hard Merch logo is featured in white on a classic black hat with our two t..." ["tax"]=> int(0) ["shipping"]=> float(7.51875) ["quantityInStock"]=> int(210) ["name"]=> string(12) "Baseball Hat" ["isShippingRequired"]=> bool(true) ["weight"]=> float(1.2) ["trackQuantity"]=> bool(true) ["fixedShippingRateOnly"]=> bool(false) ["imageUrl"]=> string(64) "https://dpbfm6h358sh7.cloudfront.net/images/1039296/51111412.jpg" ["smallThumbnailUrl"]=> string(64) "https://dpbfm6h358sh7.cloudfront.net/images/1039296/51111415.jpg" ["fixedShippingRate"]=> int(0) ["digital"]=> bool(false) ["productAvailable"]=> bool(true) ["couponApplied"]=> bool(false) } } Edited January 12, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529505 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) The "result" above in the last post is outputting only the [items] array. I am guessing i need multiple foreach loops to get all data for one order and then combine for csv export. Here is the full json result for the above order. {"total":1,"count":1,"offset":0,"limit":100,"items":[{"vendorOrderNumber":"0137065","subtotal":84,"total":104.05,"email":"pawrpr@hotmail.com","externalTransactionId":"9C831354FS0561516","paymentModule":"PayPalStandard","paymentMethod":"PayPal / Credit Card","tax":0,"ipAddress":"174.102.38.165","couponDiscount":0,"paymentStatus":"PAID","paymentMessage":"Your order has been approved","fulfillmentStatus":"AWAITING_PROCESSING","orderNumber":7065,"refererUrl":"http://fvhardmerchandise.com/shop.php","orderComments":"I'm hoping to receive this by 1/21/16 in order to give it as part of a surprise birthday gift for my husband's 60th. I booked a 7/7/16 tuna charter as his gift and want him to be able to open this as part of his surprise. If I can't get it in time, please call me at 513-317-8425.<br>Thank you! Patty","volumeDiscount":0,"customerId":20712155,"membershipBasedDiscount":0,"totalAndMembershipBasedDiscount":0,"discount":0,"usdTotal":104.05,"globalReferer":"http://fvhardmerchandise.com/charters.php","createDate":"2016-01-12 12:28:12 +0000","updateDate":"2016-01-12 12:31:40 +0000","createTimestamp":1452601692,"updateTimestamp":1452601900,"items":[{"id":69696462,"productId":9673066,"categoryId":2455976,"price":22,"productPrice":22,"sku":"00006","quantity":2,"shortDescription":"The original. Worn by Captain Marciano and his First Mate Jay from the very first season of Wicked Tuna , and from the ...","tax":0,"shipping":12.53125,"quantityInStock":85,"name":"Black T-Shirt","isShippingRequired":true,"weight":2,"trackQuantity":true,"fixedShippingRateOnly":false,"imageUrl":"https://dpbfm6h358sh7.cloudfront.net/images/1039296/36454966.jpg","smallThumbnailUrl":"https://dpbfm6h358sh7.cloudfront.net/images/1039296/36454968.jpg","fixedShippingRate":0,"digital":false,"productAvailable":true,"couponApplied":false,"selectedOptions":[{"name":"Size","value":"2X-Large","valuesArray":["2X-Large"],"type":"CHOICE"}]},{"id":69696463,"productId":15479536,"categoryId":2455976,"price":20,"productPrice":20,"sku":"00096","quantity":2,"shortDescription":"Hard Merchandise hats are here! Fan favorite Hard Merch logo is featured in white on a classic black hat with our two t...","tax":0,"shipping":7.518749999999999,"quantityInStock":210,"name":"Baseball Hat","isShippingRequired":true,"weight":1.2,"trackQuantity":true,"fixedShippingRateOnly":false,"imageUrl":"https://dpbfm6h358sh7.cloudfront.net/images/1039296/51111412.jpg","smallThumbnailUrl":"https://dpbfm6h358sh7.cloudfront.net/images/1039296/51111415.jpg","fixedShippingRate":0,"digital":false,"productAvailable":true,"couponApplied":false}],"billingPerson":{"name":"Patricia Walterman","companyName":"","street":"6053 Gaines Road","city":"Cincinnati","countryCode":"US","countryName":"United States","postalCode":"45247","stateOrProvinceCode":"OH","stateOrProvinceName":"Ohio","phone":"513-317-8425"},"shippingPerson":{"name":"Patricia Walterman","companyName":"","street":"6053 Gaines Road","city":"Cincinnati","countryCode":"US","countryName":"United States","postalCode":"45247","stateOrProvinceCode":"OH","stateOrProvinceName":"Ohio","phone":"513-317-8425"},"shippingOption":{"shippingCarrierName":"U.S.P.S.","shippingMethodName":"U.S.P.S. Priority Mail 2-Day™","shippingRate":20.05,"estimatedTransitTime":"1-3"},"handlingFee":{"name":"Handling Fee","value":0,"description":""},"additionalInfo":{},"paymentParams":{},"hidden":false}]} What is really confusing me if I am looking at this correct is that to me it looks like ['items'] is the main key of the whole order, but is also another key under the order and the array is only printing the inner ['items'] Edited January 12, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529507 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 You will need at least one more but, depending on what you're doing with the CSV you could get away with using implode() for some of it too. how much info do you need in the CSV? I assume you don't need things like thumbnails, so what's the CSV designed to do? Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529510 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) The above json is an order export from the Ecwid shopping cart for my clients orders. I am looking to create a csv file to import into our fulfillment order processing system. Right now we can manually export orders from the front end and drop that csv in a folder that auto imports them. End result would be looking for the script to export the orders hourly and drop in that ftp folder to automate the process. So you are correct i wouldn't need things like thumbnails, but the more info i can get out and into our fulfillment house's system the more reporting for the client I will be able to provide for their orders. Thanks again. Ryan Edited January 12, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529511 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 ahh, so how about just scripting the app to insert directly into the fulfillment db and cut out the middle man? You could take the data, open a connection to the fulfillment db and insert directly into the tables there. making this into a CSV means that there is going to be a lot of redundant data in the file - such as everything in the top level items array like email address and total cost, vendor id etc. will need to be entered for every order line. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529515 Share on other sites More sharing options...
mac_gyver Posted January 12, 2016 Share Posted January 12, 2016 when you var_dump the json decoded array, the whole thing if you need to, add echo '<pre>'; before the var_dump() statement so that the output will be human readable. then you need to identify which elements you need from the data. for once-per-order data, you would just access the correct array elements. for repeated data, you would loop over the correct array elements. if you need more specific help, you will need to show us or tell us specifically what elements of the array you need, show us the code you tried, show us what wrong result you got, and what result you expected from that data. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529516 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 ahh, so how about just scripting the app to insert directly into the fulfillment db and cut out the middle man? You could take the data, open a connection to the fulfillment db and insert directly into the tables there. making this into a CSV means that there is going to be a lot of redundant data in the file - such as everything in the top level items array like email address and total cost, vendor id etc. will need to be entered for every order line. Well as mentioned I am new to php and web services. Usually just front end development for ecommerce and the company sites. So taking this on may be more than I was thinking it was going to be. That being said our fulfillment system (VeraCore) does use web services via XML/SOAP/WSDL/UDDI and may be the better route for a direct connection over a csv export import type of system. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529519 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) I would think these are the items i would need to export and get into the fulfillment software to process the order and have the extra info for reporting capabilities. $output['vendorOrderNumber'] = $item['vendorOrderNumber']; $output['total'] = $item['total']; $output['email'] = $item['email']; $output['paymentMethod'] = $item['paymentMethod']; $output['tax'] = $item['tax']; $output['paymentStatus'] = $item['paymentStatus']; $output['fulfillmentStatus'] = $item['fulfillmentStatus']; $output['orderNumber'] = $item['orderNumber']; $output['orderComments'] = $item['orderComments']; $output['customerId'] = $item['customerId']; $output['discount'] = $item['discount']; $output['usdTotal'] = $item['usdTotal']; $output['createTimestamp'] = $item['createTimestamp']; //['items'] $output['id'] = $item['items'][0]['id']; $output['productId'] = $item['items'][0]['productId']; $output['categoryId'] = $item['items'][0]['categoryId']; $output['price'] = $item['items'][0]['price']; $output['productPrice'] = $item['items'][0]['productPrice']; $output['weight'] = $item['items'][0]['weight']; $output['sku'] = $item['items'][0]['sku']; $output['quantity'] = $item['items'][0]['quantity']; $output['shipping'] = $item['items'][0]['shipping']; $output['quantityInStock'] = $item['items'][0]['quantityInStock']; $output['name'] = $item['items'][0]['name']; $output['productAvailable'] = $item['items'][0]['productAvailable']; //['selectedOptions'] $output['name'] = $item['selectedOptions'][0]['name']; $output['value'] = $item['selectedOptions'][0]['value']; $output['valuesArray'] = $item['selectedOptions'][0]['valuesArray']; $output['Big'] = $item['selectedOptions'][0]['Big']; $output['type'] = $item['selectedOptions'][0]['type']; // THERE ARE MORE SELECTED OPTIONS BUT I AM LOST AT HOW THEY SHOULD BE ADDED TO THE ARRAY. //['shippingPerson'] $output['name'] = $item['shippingPerson'][0]['name']; $output['companyName'] = $item['shippingPerson'][0]['companyName']; $output['street'] = $item['shippingPerson'][0]['street']; $output['city'] = $item['shippingPerson'][0]['city']; $output['countryCode'] = $item['shippingPerson'][0]['countryCode']; $output['countryName'] = $item['shippingPerson'][0]['countryName']; $output['postalCode'] = $item['shippingPerson'][0]['postalCode']; $output['stateOrProvinceCode'] = $item['shippingPerson'][0]['stateOrProvinceCode']; $output['stateOrProvinceName'] = $item['shippingPerson'][0]['stateOrProvinceName']; $output['phone'] = $item['shippingPerson'][0]['phone']; and again these keys are taken from the response example in the api: http://developers.ecwid.com/api-documentation#orders Edited January 12, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529520 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 If it takes in xml you're laughing, use simpleXML with the PHP to create a full XML object to push straight through: as much as I hate the site, stackoverflow has a nice example of this :here: Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529524 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 (edited) Thanks i will see what I can figure out using the simpleXML example. What is the <root> mean for the SimpleXMLElement? $url = 'https://app.ecwid.com/api/v3/{STOREID}/orders?paymentStatus=PAID&fulfillmentStatus=AWAITING_PROCESSING&token={TOKEN}'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); $json_obj = json_decode($data, true); $xml = new SimpleXMLElement('<root/>'); array_walk_recursive($json_obj, array ($xml, 'addChild')); print $xml->asXML(); Result: <2>total<2>count<0>offset<100>limit<0137064>vendorOrderNumber<44>subtotal<78.3>totalemail<51608869UC712171W>externalTransactionIdpaymentModulepaymentMethod<0>tax<110.92.125.18>ipAddress<0>couponDiscountpaymentStatuspaymentMessagefulfillmentStatus<7064>orderNumberrefererUrl<0>volumeDiscount<24249682>customerId<0>membershipBasedDiscount<0>totalAndMembershipBasedDiscount<0>discount<78.3>usdTotalglobalReferer<29:32 +0000>createDate<31:00 +0000>updateDate<1452500972>createTimestamp<1452501060>updateTimestamp<69590723>id<9673066>productId<2455976>categoryId<22>price<22>productPrice<00003>sku<2>quantityshortDescription<0>tax<34.3>shipping<10>quantityInStockname<1>isShippingRequired<1.5>weight<1>trackQuantityimageUrlsmallThumbnailUrl<0>fixedShippingRate<1>productAvailablenamevalue0typenamestreetcitycountryCodecountryName<541330>postalCodestateOrProvinceCodestateOrProvinceName<+65 91698222>phonenamestreetcitycountryCodecountryName<541330>postalCodestateOrProvinceCodestateOrProvinceName<+65 91698222>phoneshippingCarrierNameshippingMethodName<34.3>shippingRatename<0>value<0137062>vendorOrderNumber<79>subtotal<138.65>totalemail<96U34224JE632150D>externalTransactionIdpaymentModulepaymentMethod<0>tax<80.217.64.221>ipAddress<0>couponDiscountpaymentStatuspaymentMessagefulfillmentStatus<7062>orderNumberrefererUrl Thank you Anna>orderComments Thank you Anna><0>volumeDiscount<24198510>customerId<0>membershipBasedDiscount<0>totalAndMembershipBasedDiscount<0>discount<138.65>usdTotalglobalReferer<00:20 +0000>createDate<04:15 +0000>updateDate<1452261620>createTimestamp<1452261855>updateTimestamp<69399848>id<51555117>productId<2455976>categoryId<40>price<40>productPrice<00604>sku<1>quantityshortDescription<0>tax<17.289855072464>shipping<23>quantityInStockname<1>isShippingRequired<2>weight<1>trackQuantityimageUrlsmallThumbnailUrl<0>fixedShippingRate<1>productAvailablenamevalue0type<69399849>id<15479536>productId<2455976>categoryId<20>price<20>productPrice<00096>sku<1>quantityshortDescription<0>tax<10.373913043478>shipping<211>quantityInStockname<1>isShippingRequired<1.2>weight<1>trackQuantityimageUrlsmallThumbnailUrl<0>fixedShippingRate<1>productAvailable<69399850>id<37228667>productId<2455976>categoryId<15>price<15>productPrice<30701>sku<1>quantityshortDescription<0>tax<30.257246376812>shipping<104>quantityInStockname<1>isShippingRequired<3.5>weight<1>trackQuantityimageUrlsmallThumbnailUrl<0>fixedShippingRate<1>productAvailablenamevalue0type<69399851>id<46912544>productId<2455976>categoryId<2>price<2>productPrice<00421>sku<1>quantityshortDescription<0>tax<0.86449275362319>shipping<59>quantityInStockname<1>isShippingRequired<0.1>weight<1>trackQuantityimageUrlsmallThumbnailUrl<0>fixedShippingRate<1>productAvailable<69399852>id<46912548>productId<2455976>categoryId<2>price<2>productPrice<00423>sku<1>quantityshortDescription<0>tax<0.86449275362319>shipping<44>quantityInStockname<1>isShippingRequired<0.1>weight<1>trackQuantityimageUrlsmallThumbnailUrl<0>fixedShippingRate<1>productAvailablenamestreetcitycountryCodecountryName<12371>postalCodestateOrProvinceCodestateOrProvinceName<0046707340555>phonenamestreetcitycountryCodecountryName<12371>postalCodestateOrProvinceCodestateOrProvinceName<0046707340555>phoneshippingCarrierNameshippingMethodName<59.65>shippingRate<6-10>estimatedTransitTimename<0>value Edited January 12, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529529 Share on other sites More sharing options...
reldridge Posted January 12, 2016 Author Share Posted January 12, 2016 Looks like xml validator is showing errors because of the hard returns used in the [orderComments]. There a way to fix this or do i need to see if i can restrict the hard returns on the front end? http://codebeautify.org/xmlvalidate Error on-> Line :1 Column :2Message :StartTag: invalid element nameError on-> Line :1 Column :2Message :Extra content at the end of the document Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529532 Share on other sites More sharing options...
Muddy_Funster Posted January 13, 2016 Share Posted January 13, 2016 The <root/> sets the top level container on the xml, it can be anything you need it to be such as "<?xml version="1.0" encoding="UTF-8" standalone="no" ?><document/>" should give you a cXML valid document object (can't vouch for the children obviously...). Also, you'll see that the values and keys are swapped, array_flip() should sort that out for you. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529558 Share on other sites More sharing options...
reldridge Posted January 13, 2016 Author Share Posted January 13, 2016 Thanks again Muddy' I will look into flipping the array. One last thing hopefully.... Do you know how i can suppress the line breaks in the [orderComments]? It is giving me an parse error because of these hard returns. $json_obj = preg_replace('\n', " ", utf8_encode($data)); $json_obj = json_decode($data, true); The above didn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529577 Share on other sites More sharing options...
reldridge Posted January 13, 2016 Author Share Posted January 13, 2016 Thanks again Muddy' I will look into flipping the array. One last thing hopefully.... Do you know how i can suppress the line breaks in the [orderComments]? It is giving me an parse error because of these hard returns. $json_obj = preg_replace('\n', " ", utf8_encode($data)); $json_obj = json_decode($data, true); The above didn't seem to work. Fixed the issue with the below code: $data = str_replace("<br>", " ", $data); Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529588 Share on other sites More sharing options...
reldridge Posted January 13, 2016 Author Share Posted January 13, 2016 (edited) If it takes in xml you're laughing, use simpleXML with the PHP to create a full XML object to push straight through: as much as I hate the site, stackoverflow has a nice example of this :here: Thanks Muddy - I was able to get the xml export a lot easier than trying to get the data to csv format. // function to convert multi-dimensional array to xml function array2XML($obj, $array) { foreach ($array as $key => $value) { if(is_numeric($key)) $key = 'item' . $key; if (is_array($value)) { $node = $obj->addChild($key); array2XML($node, $value); } else { $obj->addChild($key, htmlspecialchars($value)); } } } $xml = new SimpleXMLElement('<root/>'); array2XML($xml, $json_obj); echo (($xml->asXML('data.xml')) ? 'Your XML file has been generated successfully!' : 'Error generating XML file!'); //Store data as a string to send to database //$data = $xml->asXML(); I can now move on to looking at making a connection and passing this data off to the fulfillment system. Any info to point me into making a SOAP connection and transferring my data to their format would be great. Again thanks for all the help. Edited January 13, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529601 Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2016 Share Posted January 14, 2016 you could look at the php socket_ function list, that's where I would start. Quote Link to comment https://forums.phpfreaks.com/topic/300277-nested-json-to-php-array-for-csv-export/#findComment-1529628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.