2014-01-17 42 views
0

我试图完成这个功能,(WORDPRESS WOOCOMMERCE命令导出为JSON格式并发送为POST)我现在得到了我的JSON数据,但不是我需要的。我得到JSON在2线 [JSON数据....] [JSON数据2 ...]但我需要这样[JSON数据,JSON数据2,JSON数据3 ....]发送JSON数据作为POST使用CURL函数

我是如何产生这种输出...
查询和代码

<?php 
    //query 
      global $woocommerce; 
      global $wpdb; 
      $args = array(
       'post_type'   => 'shop_order', 
       'orderby' => 'ID', 
       'post_status'  => 'publish', 
       'posts_per_page' => -1, 
       'tax_query' => array(array(
           'taxonomy' => 'shop_order_status', 
           'field' => 'slug', 
           'terms' => array('processing')))); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post(); 
      $order_id = $loop->post->ID; 
      $order = new WC_Order($order_id); 
      $items = $order->get_items(); ?> 

而且$ JSON =阵列(....内容)是这里

<?php 
     //Colecting all the datas in array 
     // All the information of the customer 
     $json_full = array(); 
     $json=array(
     "salutation"=>''. $order->order_custom_fields['_billing_titel'][0] .'' , 
     "title"=>''. $order->order_custom_fields['_billing_anrede'][0] .'', 
     "first_name"=>''. $order->order_custom_fields['_billing_first_name'][0] .'', 
     "last_name"=>''. $order->order_custom_fields['_billing_last_name'][0] .'', 
     "street"=>''. $order->order_custom_fields['_billing_address_1'][0] .'', 
     "street_number"=>''.$order->order_custom_fields['_billing_address_2'][0] .'', 
     "address_supplement"=>''. $order->order_custom_fields['_billing_company'][0] .'', 
     "zipcode"=>''. $order->order_custom_fields['_billing_postcode'][0] .'', 
     "city"=>''. $order->order_custom_fields['_billing_city'][0] .'', 
     "country"=>''.$pais.'', 
     "terms_accepted"=>true, 
     "receiving_mails_accepted"=>$email, 
     "email"=>''. $order->order_custom_fields['_billing_email'][0] .'', 
     "lottery_accepted"=> false, 
     "lottery_solution"=> "LOTTERY", 
     "original_created_at"=>''.date('Y-m-d\TH:i:s', strtotime($order->order_date)).'', 
     ); 
     // $json = array_map('htmlentities',$json); 
     //$json = html_entity_decode(json_format($json)); 
     //Getting order name and quantity 
      // $productmeta = new WC_Product($id); 
      //$sku = $productmeta->post->sku; 
     foreach($items as $item) 
       { 
       $sku = $wpdb->get_var($wpdb->prepare('SELECT meta_value FROM dsr_postmeta WHERE meta_key="_sku"       AND post_id='.$item['product_id'].'')); 
       $sku_s = explode('-', $sku); 
       $camp = $sku_s[1]; 
       $itm_n = $sku_s[0]; 
       $json['items'][] = array (
       "campaign_number"=>(int)$camp, 
       "item_number" =>(int)$itm_n, // Here appear the name of the product 
       //'item_number' => $item['qty']); // here the quantity of the product 
      );}array_push($json_full, $json);?> 


我的输出中

[{"salutation":"Prof","title":"herr","first_name":"Michael","last_name":"Schumacher","street":"Insekammer","street_number":"2","address_supplement":"Media","zipcode":"81569","city":"Munich","country":"Deutschland","terms_accepted":true,"receiving_mails_accepted":true,"email":"[email protected]","lottery_accepted":false,"lottery_solution":"LOTTERY","original_created_at":"2014-01-16T23:13:12","items":[{"campaign_number":66,"item_number":481}]}] 

[{"salutation":"Dr.","title":"herr","first_name":"Julia","last_name":"G\u00f3mez","street":"c\/ luis cernudatte","street_number":"1","address_supplement":"Internauticos","zipcode":"SALTERAS","city":"Sevilla","country":"Spanien","terms_accepted":true,"receiving_mails_accepted":true,"email":"[email protected]","lottery_accepted":false,"lottery_solution":"LOTTERY","original_created_at":"2014-01-07T12:42:50","items":[{"campaign_number":66,"item_number":481}]}] 

但是,我真的需要的是

Like this example 

[ 
    { 
     "salutation": "Herr", 
     "title": "Prof.", 
     "first_name": "Max", 
     "last_name": "Schulz", 
     "street": "Geranienweg", 
     "street_number": "11", 
     "address_supplement": "Robinienstr. 23", 
     "zipcode": "47509", 
     "city": "Berlin", 
     "country": "Deutschland", 
     "terms_accepted": true, 
     "receiving_mails_accepted": false, 
     "email": "[email protected]", 
     "lottery_accepted": false, 
     "lottery_solution": "LOTTERY", 
     "original_created_at": "2013-10-30T18:10:28+01:00", 
     "items": [ 
      { 
       "campaign_number": 393, 
       "item_number": 2 
      }, 
      { 
       "campaign_number": 394, 
       "item_number": 5 
      } 
     ] 
    }, <---! here continuation of another order. 
    { 
     "salutation": "Herr", 
     "first_name": "Frank", 
     "street_number": "11", 
     "zipcode": "47509", 
     "city": "Berlin", 
     "country": "Deutschland", 
     "terms_accepted": true, 
     "items": [ 
      { 
       "campaign_number": 393, 
       "item_number": 2 
      } 
     ] 
    } 
]<- here the end of the ALL orders. 

我对发送的帖子服务器代码,我不知道是否是正确的,但响应服务器是

<?php 
$url = "url"; 
     $content = json_encode($json_full); 
     $curl = curl_init($url); 
     curl_setopt($curl, CURLOPT_HEADER, false); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl, CURLOPT_HTTPHEADER, 
       array("Content-type: application/json")); 
     curl_setopt($curl, CURLOPT_POST, true); 
     curl_setopt($curl, CURLOPT_POSTFIELDS, $content); 
     $json_response = curl_exec($curl); 
     $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
     if ($status != 201) { 
      die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); 
     } 
     curl_close($curl); 
     $response = json_decode ?> 

在我输出我喜欢块,我认为这是因为循环或同时的wordpress我会感谢您的帮助

回答

0

做一个新变量$ json = array()并为每个值设置$ json [] = array([values])。 不要使用array_push。

+0

你能解释pleaseeee :) – Deimos

+0

$ json [] = array(“first_name”=>“Max”,“last_name”=>“Schulz”); $ json [] = array(“first_name”=>“John”,“last_name”=>“Schulz”); 等等... – erickmcarvalho