reldridge Posted January 12, 2016 Share Posted January 12, 2016 (edited) Trying to convert json to PHP array, and having issues with line breaks in the json output from Ecwid. Ecwid Shopping Cart (API) - GET Ordershttp://developers.ecwid.com/api-documentation#orders PHP using curl to get json data of new orders <?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); $json_obj = preg_replace('\n', '', utf8_encode($data)); $json_obj = json_decode($data, true); foreach ($json_obj['items'] as $item) { echo "<pre>"; print_r($item); } ?> Array Output: (please see [orderComments]) Array ( [vendorOrderNumber] => 0137062 [subtotal] => 79 [total] => 138.65 [email] => annaschutt@hotmail.com [externalTransactionId] => 96U34224JE632150D [paymentModule] => PayPalStandard [paymentMethod] => PayPal / Credit Card [tax] => 0 [ipAddress] => 80.217.64.221 [couponDiscount] => 0 [paymentStatus] => PAID [paymentMessage] => Your order has been approved [fulfillmentStatus] => AWAITING_PROCESSING [orderNumber] => 7062 [refererUrl] => fbtab:https://www.facebook.com/fvhardmerchandise/?sk=app_251458316228 [orderComments] => If its possible to get an autograph from the captain and the crew to my husband Amadeus it would be great. He just loves the show on tv and is also a fisherman here in Sweden. Thank you Anna [volumeDiscount] => 0 [customerId] => 24198510 [membershipBasedDiscount] => 0 [totalAndMembershipBasedDiscount] => 0 [discount] => 0 [usdTotal] => 138.65 [globalReferer] => https://staticxx.facebook.com/platform/page_proxy/r/hv09mZVdEP8.js [createDate] => 2016-01-08 14:00:20 +0000 [updateDate] => 2016-01-08 14:04:15 +0000 [createTimestamp] => 1452261620 [updateTimestamp] => 1452261855 [items] => Array ( [0] => Array ............ Edited January 12, 2016 by reldridge Quote Link to comment https://forums.phpfreaks.com/topic/300297-php-array-json_decode-remove-line-breaks/ Share on other sites More sharing options...
requinix Posted January 12, 2016 Share Posted January 12, 2016 Okay, so... what issues? Are you trying to show the comment in HTML and you're not seeing the line breaks? Use nl2br. If not that, what? Quote Link to comment https://forums.phpfreaks.com/topic/300297-php-array-json_decode-remove-line-breaks/#findComment-1529543 Share on other sites More sharing options...
reldridge Posted January 13, 2016 Author Share Posted January 13, 2016 I need to take this array and turn it into XML, but there is a parse error because of the line breaks in the [orderComments]. I need to figure out how to strip these out. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/300297-php-array-json_decode-remove-line-breaks/#findComment-1529575 Share on other sites More sharing options...
reldridge Posted January 13, 2016 Author Share Posted January 13, 2016 Using str_replace instead I was able to fix this issue. $data = str_replace("<br>", " ", $data); $json_obj = json_decode($data, true); Quote Link to comment https://forums.phpfreaks.com/topic/300297-php-array-json_decode-remove-line-breaks/#findComment-1529590 Share on other sites More sharing options...
requinix Posted January 13, 2016 Share Posted January 13, 2016 What parse errors? Line breaks are totally allowed in XML. <orderComments>If its possible to get an autograph from the captain and the crew to my husband Amadeus it would be great. He just loves the show on tv and is also a fisherman here in Sweden. Thank you Anna</orderComments>If you called nl2br on the comment and then put it into XML, that would not work because would be interpreted as an XML node when you didn't want it to be. Quote Link to comment https://forums.phpfreaks.com/topic/300297-php-array-json_decode-remove-line-breaks/#findComment-1529602 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.