0

Long story short, i have the following code which is gettin info about an order from api in json, and should send this order to another system through api. All steps works good, but my problem it's if i have more products on order, my script it's creating a new order for each one.

SCRIPT:

    foreach($data['orders'] as $key => $val)
    {
        $phone = $val['phone'];
        $email = $val['email'];
        $fullname = $val['invoice_fullname'];    
        $invoicecompany = $val['invoice_company'];
        $invoicenip = $val['invoice_nip'];
        $invoiceaddress = $val['invoice_address'];
        $invoicecity = $val['invoice_city'];
        $deliveryprice = $val['delivery_price'];
        $deliverymethod = $val['delivery_method'];
    }
    
    foreach($data['orders'][0]['products'] as $key => $val){
        $pidBl = $val['product_id'];
        $ean = $val['ean'];
        $grossprice = $val['price_brutto'];
        $vatrate = $val['tax_rate'];
        $quantity = $val['quantity'];
    
    
    
    //open db
    $dbc = mysqli_connect ($dbhost, $dbuser, $dbpassword);
    $dbSelected = mysqli_select_db($dbc , $database);
    $q = $dbc->query("SELECT pId FROM offers WHERE pBarcode = '$ean';");
    
    while ($rs = $q->fetch_assoc()) {
        $pId = $rs['pId'];
    
    
    }
    $arr = [
            "objects" => [],
            "returnColumns" => []
    
        ];
    $arr['objects'][] = [
    "name" => $fullname,
    "uniqueCode" => $invoicenip,
    "active" => "true"
    ];
    $arr['returnColumns'][] = ["name" => "id"];
    
    
    
    $preparesedona = json_encode($arr);
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'BLABLABLA',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => $preparesedona,
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'Authorization: Basic BLABLABLA',
        'Content-Type: application/json'
      ),
    ));
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    $data = json_decode($response, true);
    
    
    foreach($data['result'] as $key => $val){
    $customerId = $val['id'];
    }
    
    
    
    $arr = [
            "objects" => [],
            "returnColumns" => []
    
        ];
    
    
    $arr['objects'][] = [
    "client_id" => $customerId,
    "user_id" => 1,
    "administration_id" => 1,
    "status" => 1,
    "deliveryType" => 1,
    "details" => []
    ];
    
    $arr['objects'][0]['details'][] =  [
    
    "product_id" => $pId,
    "unitPriceWithVat" => $grossprice,
    "orderedUnits" => $quantity
    
    ];
    
    $arr['returnColumns'][] = ["name" => "id"];
    
    
    $preparesedona = json_encode($arr);
    
    
     $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'BLABLABLA',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => $preparesedona,
      CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'Authorization: Basic BLABLABLA',
        'Content-Type: application/json'
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
    
    }

Yas, i know that somewhere it's a small thing which is making difference, but trust me, i tried in any possible way, i tried to look on similar errors and problems but i cannot find anything related cause i have multiple arrays which should be defined and filled...

This is how it looks my postfields

{
   "objects":[
      {
         "client_id":21947,
         "user_id":1,
         "administration_id":1,
         "status":1,
         "deliveryType":1,
         "details":[
            {
               "product_id":"11407",
               "unitPriceWithVat":159.7,
               "orderedUnits":1
            }
         ]
      }
   ],
   "returnColumns":[
      {
         "name":"id"
      }
   ]
}{
   "objects":[
      {
         "client_id":21948,
         "user_id":1,
         "administration_id":1,
         "status":1,
         "deliveryType":1,
         "details":[
            {
               "product_id":"14575",
               "unitPriceWithVat":45.31,
               "orderedUnits":1
            }
         ]
      }
   ],
   "returnColumns":[
      {
         "name":"id"
      }
   ]
}

This is how should look like:

{
   "objects":[
      {
         "client_id":21947,
         "user_id":1,
         "administration_id":1,
         "status":1,
         "deliveryType":1,
         "details":[
            {
               "product_id":"11407",
               "unitPriceWithVat":159.7,
               "orderedUnits":1
            },
            {
               "product_id":"14575",
               "unitPriceWithVat":45.31,
               "orderedUnits":1
            }
         ]
      }
   ],
   "returnColumns":[
      {
         "name":"id"
      }
   ]
}

I would much appreciate your help and please, be sympathetic, i'm not a pro, i just try to learn and i promise that i do my best :)

2
  • Given that both orders have a different client_id value, why do you want to merge them? Commented Oct 1, 2022 at 23:01
  • HI Nick, my bad, mb i didn't explain very well, i need to be created just one customer, but yesterday i was very tired and i was blind for simplest solution... fixed :D Commented Oct 2, 2022 at 4:51

1 Answer 1

0

this works for me, baiscally, i make one call to API1 to get general info about order and creating customer in API2 // after that i made another call to API1 to get info about products and this is how i structured my script and it's workin very well.

// gettin info about order data
foreach($data['orders'] as $key => $val)
{
    $phone = $val['phone'];
    $email = $val['email'];
    $fullname = $val['invoice_fullname'];    
    $invoicecompany = $val['invoice_company'];
    $invoicenip = $val['invoice_nip'];
    $invoiceaddress = $val['invoice_address'];
    $invoicecity = $val['invoice_city'];
    $deliveryprice = $val['delivery_price'];
    $deliverymethod = $val['delivery_method'];
    $deliveryaddress = $val ['delivery_address'];

// CREATE CUSTOMER -------------------------


$arr = [
        "objects" => [],
        "returnColumns" => []

    ];
$arr['objects'][] = [
"name" => $fullname,
"uniqueCode" => $invoicenip,
"active" => "true"
];
$arr['returnColumns'][] = ["name" => "id"];



$preparesedona = json_encode($arr);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'BLABLABLABLA',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $preparesedona,
  CURLOPT_HTTPHEADER => array(
    'accept: application/json',
    'Authorization: Basic BLABLABLABLA',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);
curl_close($curl);

$data = json_decode($response, true);

$customerId = $data['result'][0]['id'];

}
// Call again to get data for products

 $methodParams = [
    'order_id' => $orderidbl
];

$apiParams = [
    'method'     => 'getOrders',
    'parameters' => json_encode($methodParams),
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'BLABLABLABLABLABLABLABLA');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-BLToken: BLABLABLABLA-BLABLABLABLA-BLABLABLABLA"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams));

$output = curl_exec($ch);
curl_close($ch);
$data = json_decode($output, true);


$arr = [
        "objects" => [],
        "returnColumns" => []

       ];

$arr['objects'][] = [
"client_id" => $customerId,
"user_id" => 1,
"administration_id" => 1,
"status" => 1,
"deliveryType" => 1,
"deliveryAddress" => $deliveryaddress,
"details" => []
];

$arr['returnColumns'][] = ["name" => "id"];

// prepare products area "details"

foreach($data['orders'][0]['products'] as $key => $val){
    $pidBl = $val['product_id'];
    $ean = $val['ean'];
    $grossprice = $val['price_brutto'];
    $vatrate = $val['tax_rate'];
    $quantity = $val['quantity'];



//open db
$dbc = mysqli_connect ($dbhost, $dbuser, $dbpassword);
$dbSelected = mysqli_select_db($dbc , $database);
$q = $dbc->query("SELECT pId FROM offers WHERE pBarcode = '$ean';");

while ($rs = $q->fetch_assoc()) {
    $pId = $rs['pId'];
}


$arr['objects'][0]['details'][] =  [

"product_id" => $pId,
"unitPriceWithVat" => $grossprice,
"orderedUnits" => $quantity

];
}

$preparesedona = json_encode($arr);

var_dump($preparesedona);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.