Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MarkLeeuwesteijn's avatar

Foreach in cURL result

Hi All,

I get stuck on a small piece in my API post. Will you guys help with this? The idea is that under "items" I get the output of my packageDetails (this can be multiple items).

Here's the code:

$url = "https://website.com";

    $request = [
        'username' => '',
        'password' => '',
        'environment'   => 'development',
        "action" => "quote",
        "shipment" => array(
            "o_company" =>          $this->o_company,
            "o_name" =>             $this->o_name,
            "o_email" =>            $this->o_email,

            "items" => array(
			// foreach should start here to fill the "items"
                        array(
                            "contents" => "ENINGE PARTS",
                            "value" => "55.00",
                            "weight" => "5.0",
                            "length" => "50",
                            "width" => "15",
                            "height" => "5",
                            "hs_code" => "85183000",
                        ), array(
                            "contents" => "SPARE PARTS",
                            "value" => "55.00",
                            "weight" => "5.0",
                            "length" => "50",
                            "width" => "15",
                            "height" => "5",
                            "hs_code" => "85183000",
                        ),
					/// and end foreach

                    )
                )
            ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($response, true);

How can I do the foreach loop under the "items" to get the right package details?

Thank you so much!

0 likes
1 reply
MarkLeeuwesteijn's avatar
Level 1

Fixed it myself, solution below;

    $packing =  packingDetails::where('file', $this->file->id)->get()->toArray();
    $arrayLength = count($packing);
    $i = 0;
    while ($i < $arrayLength) {
        $items[] = [
            'contents' =>   $packing[$i]['line'],
            "value" =>      $packing[$i]['shipmentValue'],
            "weight" =>     $packing[$i]['shipmentWeight'],
            "length" =>     $packing[$i]['shipmentLenght'],
            "width" =>      $packing[$i]['shipmentWidth'],
            "height" =>     $packing[$i]['shipmentHeight'],
            "hs_code" =>    $packing[$i]['hsCode'],
        ];
        $i++;
    }

And then in the request;

        "items" =>  $items
            )
        ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($response, true);

Please or to participate in this conversation.