Running a foreach loop within a Curl function

Tags: ,



I need to run a foreach loop within a curl command but can’t quite get the syntax right. I’ve parsed other variables into it fine but it’s just formatting my array to fit.

   curl_setopt($ch, CURLOPT_POSTFIELDS, "{
      "order": {
        "channel_id": $channel_id,
        "customer_id": $customer_id,
        "deliver_to_id": $deliver_to_id,
        "delivery_method_id": $delivery_method_id,
        "line_items_attributes": [
          {
            "sellable_id": 39236017,
            "price_per_unit": "1.000",
            "quantity": "10"
          }
        ],
      }
    }");

This is my array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [1] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [2] => Array
                (
                    [id] => 39236029
                    [quantity] => 0
                    [price] => 2.952
                )

            [3] => Array
                (
                    [id] => 39236015
                    [quantity] => 0
                    [price] => 3.333
                )

        )

)

I need to push my array into this part specifically:

"sellable_id": 39236017,
"price_per_unit": "1.000",
"quantity": "10"

This is my attempt but this is causing all kinds of issues, where am I going wrong?

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  "order": {
    "channel_id": $channel_id,
    "customer_id": $customer_id,
    "deliver_to_id": $deliver_to_id,
    "delivery_method_id": $delivery_method_id,
    "line_items_attributes": [
      {
        foreach($parcels[0] as $item){
          "sellable_id": $item['id'],
          "price_per_unit": $item['price'],
          "quantity": $item['quantity'],
        }
      }
    ],
  }
}");

Slight addition, what if the array was this and I didn’t target it with foreach($parcels[0] as $item) { but it was foreach($parcels as $item) { instead?

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.46
                )

            [1] => Array
                (
                    [id] => 39236017
                    [quantity] => 1
                    [price] => 2.75
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 39236029
                    [quantity] => 1
                    [price] => 2.58
                )

        )

)

Answer

You can’t just put PHP code in string and expect it to be executed. You must transform your array, and then pass to cUrl:

$order = [
  "order" => [
    "channel_id" => $channel_id,
    "customer_id" => $customer_id,
    "deliver_to_id" => $deliver_to_id,
    "delivery_method_id" => $delivery_method_id,
    "line_items_attributes" => [
      [
        "sellable_id": 39236017,
        "price_per_unit": "1.000",
        "quantity": "10"
      ]
    ],
  ]
];

foreach($parcels[0] as $item) {
    $order['order']['line_items_attributes'][] = [
        "sellable_id" => $item['id'],
        "price_per_unit" => $item['price'],
        "quantity" => $item['quantity'],
    ];      
}

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order));


Source: stackoverflow