function - special character from array is not working in csv file using php -
function - special character from array is not working in csv file using php -
i have created 1 app using php used download details .csv file format shopify store.
now details got it, details special characters not working fine.
i referenced lot of stack overflow questions related issues, not helping me.
sample output:
php:
<?php require_once 'lib/shopify.php'; //require_once 'csv.php'; function csvtojson($csv) { $rows = explode("\n", trim($csv)); $csvarr = array_map(function($row) { $keys = array( 'var0', 'var1', 'var2', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9' ); homecoming array_combine($keys, str_getcsv($row)); }, $rows); $json = json_encode($csvarr); homecoming $json; } $t = "bac2486aa2b31aa5aed1fdd62e77a4ae"; $sc = new shopifyclient("mine-329.myshopify.com/", $t, api_key, secret); $order = array(); if (!isset($t)) { if (!isset($_get['signature'])) { $url = $sc->getauthorizeurl("read_orders"); header('location: ' . $url); } if (isset($_get['code'])) { $acctok = $sc->getaccesstoken($_get['code']); echo "token = " . $acctok; exit; $orders = $sc->call('get', 'admin/orders.json', array( 'published_status' => 'published' )); foreach ($orders $order) { echo $order['id']; echo $order['email']; } } } if (isset($t)) { $orders = $sc->call('get', 'admin/orders.json', array( 'published_status' => 'published' )); $myarray = array(); $list[] = array( 'name', 'email', 'financial status', 'paid at', 'fulfillment status', 'fulfilled at', 'accepts marketing', 'currency', 'subtotal', 'shipping', 'taxes', 'total', 'discount code', 'discount amount', 'shipping method', 'created at', 'lineitem quantity', 'lineitem name', 'lineitem price', 'lineitem compare @ price', 'lineitem sku', 'lineitem requires shipping', 'lineitem taxable', 'lineitem fulfillment status', 'billing name', 'billing street', 'billing address1', 'billing address2', 'billing company', 'billing city', 'billing zip', 'billing province', 'billing country', 'billing phone', 'shipping name', 'shipping street', 'shipping address1', 'shipping address2', 'shipping company', 'shipping city', 'shipping zip', 'shipping province', 'shipping country', 'shipping phone', 'notes', 'note attributes', 'vat', 'cancelled at', 'payment method', 'payment reference', 'refunded amount', 'vendor', 'id', 'tags' ); $list = array_filter($list); $empty_note_attribute = ""; if (empty($order['note_attributes'])) { $empty_note_attribute = ""; } else { $empty_note_attribute = $order['note_attributes']; } $empty_discount_codes = ""; if (empty($order['discount_codes'])) { $empty_discount_codes = ""; } else { $empty_discount_codes = $order['discount_codes']; } foreach ($orders $order) { $str = file_get_contents('slim-2.json'); //give address of json file $str = mb_convert_encoding($str, 'utf-8'); $json_country = json_decode($str, true); //here problem passing `$str` should pass `$json_country` if (is_array($json_country)) { foreach ($json_country $key => $value) { if (array_search(trim($order['billing_address']['country']), $value)) // added trim { $order['billing_address']['country'] = $value['alpha-2']; } if (isset($order['shipping_address']['country'])) { if (array_search(trim($order['shipping_address']['country']), $value)) // added trim { $order['shipping_address']['country'] = $value['alpha-2']; } } } } $csv = file_get_contents('states.csv'); $json = csvtojson($csv); $json_state = json_decode($json, true); $bilprovin = trim($order['billing_address']['province']); $shpprovin = ''; if (isset($order['shipping_address'])) { $shpprovin = $order['shipping_address']['province']; } foreach ($json_state $keys) { if (array_search($bilprovin, $keys)) // added trim { $order['billing_address']['province'] = substr($keys['var1'], 3); } if (isset($order['shipping_address'])) { if (array_search($shpprovin, $keys)) { $order['shipping_address']['province'] = substr($keys['var1'], 3); } } } $orders_lineitem = $order['line_items']; $lineitemcount = 0; foreach ($orders_lineitem $lineitem) { /* fulfillment status */ $fulfill_status = ""; if (empty($order['fulfillment_status'])) { $fulfill_status = "pending"; } else { $fulfill_status = $order['fulfillment_status']; } /* lineitem fulfillment status */ $fulfillment_status = ''; if (empty($order['fulfillments'][0]['created_at'])) { $fulfillment_status = "pending"; } else { $fulfillment_status = $order['line_items'][0]['fulfillment_status']; } $variants = ""; $variants = $sc->call('get', "admin/variants/" . $lineitem['variant_id'] . ".json", array( 'published_status' => 'published' )); $requires_shipping = "false"; if ($lineitem['requires_shipping']) { $requires_shipping = "true"; } $lineitem_taxable = "false"; if ($lineitem['taxable']) { $lineitem_taxable = "true"; } //** accepts marketting ** $accepts_marketting = "no"; if ($order['buyer_accepts_marketing']) { $accepts_marketting = "yes"; } $shipping = ''; if (count($order['shipping_lines']) > 0) { $shipping = $order['shipping_lines'][0]['price']; } /* fulfilled @ */ $created_at = ''; if (count($order['fulfillments']) > 0) { $created_at = $order['fulfillments'][0]['created_at']; } /* total taxation */ $total_tax = ''; if (count($order['line_items'][0]['tax_lines']) > 0) { $total_tax = $order['line_items'][0]['tax_lines'][0]['price']; } /* payment reference */ $payment = ''; if ($order['checkout_id']) { $payment = sprintf('c%s.1', $order['checkout_id']); } /* refunded amount */ $refund_price = ''; if (count($order['refunds']) > 0) { foreach ($order['refunds'] $refund) { if (count($refund['transactions']) > 0) { $refund_price += $refund['transactions'][0]['receipt']['paid_amount']; } } } if ('pending' == $fulfill_status || 'partial' == $fulfill_status) { $created_at = ''; } if ($lineitemcount == 0) { array_push($list, array( $order['name'], $order['email'], $order['financial_status'], (($order['financial_status'] != 'partially_refunded') && ($order['financial_status'] != 'refunded') && ($order['financial_status'] != 'authorized') ? $order['created_at'] : null), $fulfill_status, $created_at, $accepts_marketting, $order['currency'], $order['subtotal_price'], (($shipping > 0) ? $shipping : 0), $order['total_tax'], $order['total_price'], $empty_discount_codes, $order['total_discounts'], (($shipping > 0) ? $order['shipping_lines'][0]['code'] : null), $order['created_at'], $lineitem['quantity'], $lineitem['name'], $lineitem['price'], $variants['compare_at_price'], $lineitem['sku'], $requires_shipping, $lineitem_taxable, $fulfillment_status, $order['billing_address']['name'], $order['billing_address']['address1'], $order['billing_address']['address1'], $order['billing_address']['address2'], $order['billing_address']['company'], $order['billing_address']['city'], $order['billing_address']['zip'], $order['billing_address']['province'], $order['billing_address']['country'], $order['billing_address']['phone'], (($shipping > 0) ? $order['shipping_address']['name'] : null), (($shipping > 0) ? $order['shipping_address']['address1'] : null), (($shipping > 0) ? $order['shipping_address']['address1'] : null), (($shipping > 0) ? $order['shipping_address']['address2'] : null), (($shipping > 0) ? $order['shipping_address']['company'] : null), (($shipping > 0) ? $order['shipping_address']['city'] : null), (($shipping > 0) ? $order['shipping_address']['zip'] : null), (($shipping > 0) ? $order['shipping_address']['province'] : null), (($shipping > 0) ? $order['shipping_address']['country'] : null), (($shipping > 0) ? $order['shipping_address']['phone'] : null), $order['note'], $empty_note_attribute, $total_tax, $order['cancelled_at'], $order['payment_details']['credit_card_company'], $payment, (($refund_price > 0) ? $refund_price : 0), $lineitem['vendor'], $order['id'], $order['tags'] )); } else { array_push($list, array( $order['name'], $order['email'], '', '', '', '', '', '', '', '', '', '', $empty_discount_codes, '', '', $order['created_at'], $lineitem['quantity'], $lineitem['name'], $lineitem['price'], $variants['compare_at_price'], $lineitem['sku'], $requires_shipping, $lineitem_taxable, $fulfillment_status, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', $lineitem['vendor'], '', $order['tags'] )); } $lineitemcount++; } } $order = $_get; $list[] = array_to_csv_download($myarray, // array going sec row "numbers.csv"); $list = array_filter($list); array_to_csv_download($list); header("content-type: text/csv; charset=utf-16le"); header("content-disposition: attachment; filename=file.csv"); header("pragma: no-cache"); header("expires: 0"); } function array_to_csv_download($list) { $output = fopen("php://output", "w"); foreach ($list $row) { fputcsv($output, $row); //print_r($row); die; } fclose($output); } ?>
how prepare problem, , exact function use?
any help appreciated!!!
thank you.
change header
header("content-type: text/csv; charset=utf-16le"); header("content-disposition: attachment; filename=file.csv"); header("pragma: no-cache"); header("expires: 0");
to
header('content-encoding: utf-8'); header('content-type: text/csv; charset=utf-8'); header("content-disposition: attachment; filename=file.csv"); header("pragma: no-cache"); echo "\xef\xbb\xbf"; // utf-8 bom
copied here
php function csv special-characters
Comments
Post a Comment