php中的json国家代码

时间:2014-06-11 11:16:17

标签: php json

这是我的PHP代码:

<?php
require_once 'lib/shopify.php';
//require_once 'csv.php';
$t  = "bac2486aa2b31aa5aed1fdd62e77a4ae";
$sc = new ShopifyClient("mine-329.myshopify.com/", $t, API_KEY, SECRET);
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 as $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',
        'Fullfillment Status',
        'Fullfilled at',
        'Accepts Marketing',
        'Currency',
        'Subtotal',
        'Shipping',
        'Taxes',
        'Total',
        'Discount code',
        'Discount Amount',
        'Shipping Method',
        'created at',
        'Lineitem quantity',
        'Lineitem name',
        'Lineitem price',
        'compare at 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',
        'Refund',
        '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 as $order) {


$str = file_get_contents('slim-2.json');//give the address of json file  
$json_country = json_decode($str, true);
if(is_array($json_country)) {
foreach ($json_country as $key=>$value) {
if(array_search($order['billing_address']['country'], $value)) 
{
$order['billing_address']['country']=$value['alpha-2'];         
}
if(array_search($order['shipping_address']['country'], $value))         
{
$order['shipping_address']['country']=$value['alpha-2'];        
}
}
}

        $orders_lineitem = $order['line_items'];
        $lineitemcount   = 0;
        foreach ($orders_lineitem as $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 at
            */
            $created_at = '';
            if (count($order['fulfillments']) > 0) {
                $created_at = $order['fulfillments'][0]['created_at'];
            }


        /*
        total tax
        */
        $total_tax='';
        if(count($order['line_items'][0]['tax_lines']) > 0) {
        $total_tax= $order['line_items'][0]['tax_lines'][0]['price'];
        }

            /*  
            Refunded Amount 
            */
            $refund_price = '';
            if (count($order['refunds']) > 0) {
                foreach ($order['refunds'] as $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!= '0') ? $order['total_tax'] : null),
                    $order['cancelled_at'],
                    $order['payment_details']['credit_card_company'],
                    $order['checkout_id'],
                    (($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,
                    $order['line_items'][0]['fulfillment_status'],
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    $lineitem['vendor'],
                    '',
                    $order['tags']
                ));
            }
            $lineitemcount++;
        }
    }
    $order  = $_GET;
    $list[] = array_to_csv_download($myarray, // this array is going to be the second row
        "numbers.csv");
    $list   = array_filter($list);
    array_to_csv_download($list);
    header("Content-type: text/csv");
    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 as $row) {
        fputcsv($output, $row);
        //print_r($row); die;
    }
    fclose($output);
}
?>

这是我的输出:http://imgur.com/39dLW3v

在我的.csv文件中,在结算国家/地区和发货国家/地区未更改。它显示为全名。但我想要两个字符的代码(例如:IN,USA)

此代码中的错误是什么?

2 个答案:

答案 0 :(得分:0)

您正在$json_country数组中获取json但未使用它

$str = file_get_contents('slim-2.json');//give the address of json file  

//Change encoding to UTF-8
$str = mb_convert_encoding($str, 'UTF-8');

$json_country = json_decode($str, true);

//Here is the problem you are passing `$str` but you should pass `$json_country`
if(is_array($json_country)) {
  foreach ($json_country as $key=>$value) {
     if(array_search( trim($order['billing_address']['country']), $value)) // Added trim 
     {
       $order['billing_address']['country']=$value['alpha-2'];         
     }

     if(array_search( trim($order['shipping_address']['country']), $value)) // Added trim         
     {
      $order['shipping_address']['country']=$value['alpha-2'];        
     }
  }
}

更新

我测试了它并发现它是编码问题,file_get_contents正在弄乱您需要将其转换为UTF-8的编码,以便json_decode()可以解析它。

检查上面的代码现在是否正常工作。

答案 1 :(得分:0)

似乎这些行中有错误:

if(is_array($str)) {
    foreach ($str as $key=>$value) {

尝试改为:

if (is_array($json_country)) {
    foreach ($json_country as $key => $value) {