# Data Import

# Orders


To import historical orders use our endpoint "https://api.edrone.me/trace". Is important to fill "event_utc_date" field.


<?php

$history_orders = '[{
    "email": "johndoe@example.com",
    "user_id: "1234",
    "event_date": "2018-03-12T08:04:46.241168Z",
    "first_name": "John",
    "last_name": "Doe",
    "gender": "M",
    "birth_date": "1995-02-28",
    "phone": "123123123",
    "order_id": "19626",
    "city": "UK",
    "country": "London",
    "order_payment_value": "166.46",
    "order_currency": "GBP",
    "subscriber_status": "1",
    "customer_tags": ["tag1", "tag2"],
    "ordered_products": [{
        "product_id": "2356",
        "product_image": "https://example.com/img/product_media/2001-3000",
        "product_url": "https://www.example.com/plecaki/le-coq-sportif-plecak-classique-backpack",
        "product_title": "LeCoqSportifPlecakClassiqueBackpack",
        "quantity": "2",
        "product_categories": [{
            "name": "Accessories",
            "id": "227"
          },
          {
            "name": "Bags",
            "id": "331"
          }
        ]
      },
      {
        "product_id": "2339",
        "product_image": "https://example.com/img/product_media/2001-3001",
        "product_url": "https://www.example.com/portfele/le-coq-sportif-portfel-classique-wallet-2",
        "product_title": "LeCoqSportifPortfelClassiqueWallet",
        "quantity": "2",
        "product_categories": [{
            "name": "Accessories",
            "id": "227"
          },
          {
            "name": "Wallets",
            "id": "319"
          }
        ]
      },
      {
        "product_id": "20857",
        "product_image": "https://example.com/img/product_media/2001-3002",
        "product_url": "https://www.example.com/okulary-przeciwsloneczne/okulary-vans-spicoli-4-shade-white-rainbow-m",
        "product_title": "SunGlassesVansSpicoli4Shade",
        "quantity": "2",
        "product_categories": [{
            "name": "Accessories",
            "id": "227"
          },
          {
            "name": "Sun Glasses",
            "id": "328"
          }
        ]
      }
    ]
  },
  {
    "email": "alicebowman@example.com",
    "user_id: "1234",
    "event_date": "2018-03-12T08:04:46.241168Z",
    "first_name": "Alice",
    "last_name": "Bowman",
    "gender": "F",
    "birth_date": "1996-02-28",
    "phone": "123123123",
    "order_id": "19627",
    "city": "UK",
    "country": "London",
    "order_payment_value": "219.99",
    "order_currency": "GBP",
    "subscriber_status": "",
    "customer_tags": ["tag1", "tag2"],
    "ordered_products": [{
        "product_id": "18783",
        "product_image": "https://example.com/img/product_media/18001-19000",
        "product_url": "https://www.example.com/adidas-originals-2/buty-adidas-stan-smith-women-ftwr-white-2",
        "product_title": "Buty adidas Stan Smith Women",
        "quantity": "1",
        "product_categories": [{
            "name": "Shoes",
            "id": "225"
          },
          {
            "name": "Women",
            "id": "229"
          },
          {
            "name": "adidas originals",
            "id": "238"
          }
        ]
      }
    ]
  },
  {
    "email": "alicebowman@example.com",
    "user_id: "1234",
    "event_date": "2018-03-12T08:04:46.241168Z",
    "first_name": "Alice",
    "last_name": "Bowman",
    "gender": "F",
    "birth_date": "1996-02-28",
    "phone": "123123123",
    "order_id": "19627",
    "city": "London",
    "country": "UK",
    "order_payment_value": "244.98",
    "order_currency": "GBP",
    "subscriber_status": "",
    "customer_tags": ["tag1", "tag2"],
    "ordered_products": [{
        "product_id": "18261",
        "product_image": "https://example.com/img/product_media/18001-19001",
        "product_url": "https://www.example.com/adidas-originals/buty-adidas-la-trainer-og-collegiate-green",
        "product_title": "adidas LA TRAINER OG",
        "quantity": "2",
        "product_categories": [{
            "name": "Shoes",
            "id": "225"
          },
          {
            "name": "Men",
            "id": "228"
          },
          {
            "name": "adidas originals",
            "id": "237"
          }
        ]
      }
    ]
  }]';

function utcNow() {
    $t = microtime(true);
    $micro = sprintf("%03d", ($t - floor($t)) * 1000000);
    return gmdate('Y-m-d\TH:i:s.', $t) . $micro . 'Z';
  }

function prepTrace($order) {
    $products = $order->ordered_products;
    $product_ids = [];
    $product_titles = [];
    $product_counts = [];
    $product_urls = [];
    $product_images = [];
    $product_category_ids = [];
    $product_category_names = [];

    foreach ($products as $product) {
        $single_product_category_ids = [];
        $single_product_category_names = [];
        foreach($product->product_categories as $category){
          array_push($single_product_category_ids, $category->id);
          array_push($single_product_category_names, $category->name);
        }

        array_push($product_ids, $product->product_id);
        array_push($product_titles, $product->product_title);
        array_push($product_urls, $product->product_url);
        array_push($product_counts, $product->quantity);
        array_push($product_images, $product->product_image);
        array_push($product_category_ids,  join('~', $single_product_category_ids));
        array_push($product_category_names,  join('~', $single_product_category_names));


    }

    $edroneData = 'version=' . '1.0.0' .
            '&app_id=' . 'YOUR_APP_ID' .
            '&email=' . $order->email .
            '&user_id=' . $order->user_id .
            '&first_name=' . $order->first_name .
            '&last_name=' . $order->last_name .
            '&product_ids=' . join('|', $product_ids) .
            '&product_titles=' . join('|', $product_titles) .
            '&product_images=' . join('|', $product_images) .
            '&product_urls=' . join('|', $product_urls) .
            '&product_counts=' . join('|', $product_counts) .
            '&product_category_ids=' . join('|', $product_category_ids) .
            '&product_category_names=' . join('|', $product_category_names) .
            '&order_id=' . $order->order_id .
            '&order_payment_value=' . $order->order_payment_value .
            '&base_payment_value=' . $order->order_payment_value .
            '&sender_type=' . 'server' .
            '&base_currency=' . $order->order_currency .
            '&order_currency=' . $order->order_currency .
            '&action_type=' . 'order' .
            '&country=' . $order->country .
            '&city=' . $order->city .
            '&phone=' . $order->phone .
            '&customer_tags=' . join('|', $order->customer_tags) .
            '&subscriber_status=' . '' .
            '&event_utc_date=' . $order->event_date .
            '&utc_time=' . utcNow();
    return $edroneData;
}

function httpPost($url, $params) {
  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_POSTFIELDS => $params,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    // CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
      "cache-control: no-cache",
      "content-type: application/x-www-form-urlencoded"
    ),
  ));

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
  } else {
    echo $response;
  }
}

foreach (json_decode($history_orders) as $order) {
    httpPost('https://api.edrone.me/trace', prepTrace($order));
}