<?php

header('Content-type:application/json');
function generate_awb()
{
    $url = 'http://bookurier.ro/colete/serv/add_cmd.php';
    $ch = curl_init();
    $msgstr = "<msg>\r\n\t\t\t<cmd>\r\n\t\t\t<client>" . $_REQUEST['id_unic'] . "</client>\r\n\t\t\t<unq>" . $_REQUEST['nr_comanda'] . "</unq>\r\n\t\t\t<recv>" . $_REQUEST['nume_client'] . "</recv>\r\n\t\t\t<phone>" . $_REQUEST['telefon'] . "</phone>\r\n\t\t\t<email>" . $_REQUEST['email'] . "</email>\r\n\t\t\t<country>" . $_REQUEST['tara'] . "</country>\r\n\t\t\t<city>" . $_REQUEST['oras'] . "</city>\r\n\t\t\t<district>" . $_REQUEST['district'] . "</district>\r\n\t\t\t<zip>" . $_REQUEST['cod_postal'] . "</zip>\r\n\t\t\t<street>" . $_REQUEST['address_line'] . "</street>\r\n\t\t\t<service>" . $_REQUEST['service_id'] . "</service>\r\n\t\t\t<packs>" . $_REQUEST['nr_colete'] . "</packs>\r\n\t\t\t<exchange_pack>" . $_REQUEST['colet_schimb'] . "</exchange_pack>\r\n\t\t\t<weight>" . $_REQUEST['greutate'] . "</weight>\r\n\t\t\t<rbs_val>" . $_REQUEST['val_ramburs'] . "</rbs_val>\r\n\t\t\t<confirmation>" . $_REQUEST['confirmare'] . "</confirmation>\r\n\t\t\t<ret_doc>0</ret_doc>\r\n\t\t\t<ret_doc_desc></ret_doc_desc>\r\n\t\t\t<insurance_val>" . $_REQUEST['val_asigurare'] . "</insurance_val>\r\n\t\t\t<notes>" . $_REQUEST['note'] . "</notes>\r\n\t\t</cmd>\r\n\t\t</msg>";
    $msgstr = str_replace(array("\r", "\n", "\r\n", "\t"), '', $msgstr);
    $user_id = $_REQUEST['username'];
    $pwd = $_REQUEST['password'];
    $fields = array('userid' => $user_id, 'pwd' => $pwd, 'msg' => urlencode($msgstr));
    $fields_string = '';
    foreach ($fields as $key => $value) {
        $fields_string .= $key . '=' . $value . '&';
    }
    rtrim($fields_string, '&');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $msg = curl_exec($ch);
    if (count($msg)) {
        $date_awb = array('id_awb' => $msg);
        $data = json_encode($date_awb);
        return $data;
    } else {
        echo 'Eroare:Date incomplete.';
    }
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && $_SERVER["CONTENT_TYPE"] == "application/json") {
    $data = trim(file_get_contents("php://input"));
    $json = json_decode($data, true);
    if (is_array($json)) {
        $_REQUEST = $json + $_REQUEST;
    }
    echo generate_awb();
} else {
    http_response_code(405);
}