0

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
                )

        )

)

1 Answer 1

2

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));
Sign up to request clarification or add additional context in comments.

6 Comments

Also adding a note here for Rob: By using json_encode you ensure all fields and data are properly escaped and valid json. Avoid trying to 'build the string yourself'. You will just create more headaches for yourself.
@Justinas Thanks that makes sense, it's working now!
@IncredibleHat Thanks for the info too, helps explain things a bit on something I'm terrible at!!
@Justinas I've just hit upon another setup, I've updated my question, how would I incorporate that style of array? Instead of doing $parcels[0] in the foreach?
@IncredibleHat I'm not sure if you can help with the additional question above? You grasped everything quickly.
|

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.