Home > Enterprise >  How to loop through CURLOPT_POSTFIELDS data with multiple id's PHP
How to loop through CURLOPT_POSTFIELDS data with multiple id's PHP

Time:04-12

I have an order that has multiple line items (in particular 15) and I am trying to loop through id's with params What am I missing here?:

   foreach($line_ids as $key => $id ) {

      $params = [
        "order_lines" => [
          "accepted"  => true, 
          "id"        => $id
        ]
        ];

      $data = json_encode($params);
      echo '<pre>';
      print_r($data);
      echo '</pre>';

    }

the out put looks like this:

{ 
  "order_lines":{
    "accepted":true,
    "id":"75167652-1"
  }
}

but it needs to be like this for each line item:

{
  "order_lines": [
    {
      "accepted": true,
      "id": "75261431-1"
    }
  ]
}

here is the CURL for a single id:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $baseUrl . $order_id . '/accept',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'PUT',
  CURLOPT_POSTFIELDS =>'{
  "order_lines": [
    {
      "accepted": true,
      "id": "75261431-1"
    }
  ]
}
',
  CURLOPT_HTTPHEADER => array(
    'Authorization: xxxxxx-xxxxx-xxxxx-xxxxx',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

here is the CURL i've tried for multiples id's with the foreach above.:

  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => $baseUrl . $order_id . '/accept',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_HTTPHEADER => array(
      'Authorization: xxxxxx-xxxxx-xxxxx-xxxxx',
      'Content-Type: application/json'
    ),
  ));

  $accept_orders = curl_exec($curl);

  curl_close($curl);
  echo $accept_orders . '<br>';

but this is the output I get for each line item:

{ "message" : "Body is required", "status" : 400 }

CodePudding user response:

The error you get is not related to the array, it says that the data is sent empty.

It could be that you didn't enable the POST request on the curl connection causing this.


CURLOPT_POST => true

Array Example;




<?php

$ids = array(1,2,3,4,5);

$params = array();

foreach($ids as $id)
{
$params['order_lines'][] = ['id' => $id,'accepted' => 'true'];
}

$json = json_encode($params,JSON_PRETTY_PRINT);

print_r($json);

?>


{
     "order_lines": [
         {
             "id": 1,
             "accepted": "true"
         },
         {
             "id": 2,
             "accepted": "true"
         },
         {
             "id": 3,
             "accepted": "true"
         },
         {
             "id": 4,
             "accepted": "true"
         },
         {
             "id": 5,
             "accepted": "true"
         }
     ]
}


  • Related