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

MohammedSB's avatar

Laravel POST API -> cURL error 28:

Hi all, hope you are all doing well,

on my project, I faced an issue with posting data to an internal API, and keep getting cURL error 28 which means "Operation timed out after 30006 milliseconds with 0 bytes received"

BUT when I test the same API with the same values in POSTMAN it works fine, [Screenshot] -> https://tppr.me/hensp

And my code is attached below.

Thanks in advance

public function addToCart($productId, array $options = [], $quantity = 1)
    {
        $this->product = Product::findOrFail($productId);

        if (!Session::has('cart-token'))
            Session::put('cart-token', Str::random(100));

        $response = Http::post(env('APP_API_URL') . '/cart/store', [
            'productId' => $this->product->id,
            'quantity' => $quantity,
            'options' => $options,
            'cartToken' => Session::get('cart-token')
        ]);

        $this->dispatchBrowserEvent('alert', [
            'type' => $response['status'] ? 'success' : 'error',
            'message' => $response['message']
        ]);
    }

and the env variable is

APP_URL=127.0.0.1:8000
API_VERSION=1
APP_API_URL="${APP_URL}/api/v${API_VERSION}"

API controllers

public function addToCart(Request $request)
    {
        App::setLocale('en');
        $request->validate([
            'productId' => 'required|integer|exists:products,id',
            'options'   => 'array',
            'options.*' => 'distinct|integer',
            'quantity'  => 'required|integer',
            'cartToken' => 'nullable|string|max:191'
        ]);

        $this->product = Product::findOrFail($request['productId']);

        if ($request['quantity'] < 0)
            $this->condition = true;
        else
            $condition = $this->conditions($request['options'] ?? [], $request['quantity']);

        if ($this->conditionStatus) {
            try {

                $query = CartModel::where('cart_token', $request['cartToken'])
                    ->where('product_id', $this->product->id)
                    ->whereJsonContains('option_ids', $request['options'] ?? [])
                    ->firstOrFail();

                $query->update([
                    'quantity' => $query->quantity + $request['quantity']
                ]);

            } catch (\Exception $e) {

                $query = CartModel::create([
                    'cart_token'    => $request['cartToken'],
                    'product_id'    => $this->product->id,
                    'option_ids'    => $request['options'] ?? [],
                    'quantity'      => $request['quantity']
                ]);

            }

            return response()->json([
                'status' => true,
                'code' => 101,
                'cart_token' => $query->cart_token,
                'message' => trans('frontend/alert.cart.added_to_cart', ['product' => $this->product->detail->handleMultipleLanuage('title')])
            ]);
        }

        return response()->json($condition, 422);
    }

    public function conditions(array $options = [], $quantity)
    {
        $productOptions = ProductOption::where('product_id', $this->product->id)->get();

        if ($productOptions) {
            if (count($productOptions) != count($options))
                return [
                    'status' => false,
                    'code' => 107,
                    'message' => trans('frontend/alert.cart.select_option')
                ];

            $userCart = CartModel::where('option_ids', $options)->sum('quantity');
            $productOptionLists = ProductOptionList::whereIn('id', $options)->get();
            foreach ($productOptionLists as $listItem) {
                if ($listItem->quantity == 0)
                    return response()->json([
                        'status' => false,
                        'code' => 102,
                        'message' => trans('frontend/alert.cart.out_of_stock', ['product' => $this->product->detail->handleMultipleLanuage('title')])
                    ]);

                if ($listItem->quantity < $userCart + $quantity)
                    return [
                        'status' => false,
                        'code' => 103,
                        'message' => trans('frontend/alert.cart.maximum_units')
                    ];
            }
        }

        if ($this->product->stock->quantity == 0 && $this->product->stock->quantity < $quantity)
            return response()->json([
                'status' => false,
                'code' => 102,
                'message' => trans('frontend/alert.cart.out_of_stock', ['product' => $this->product->detail->handleMultipleLanuage('title')])
            ]);

        if ($this->product->stock->quantity < CartModel::where('product_id', $this->product->id)->sum('quantity') + $quantity)
            return [
                'success' => false,
                'code' => 103,
                'message' => trans('frontend/alert.cart.maximum_units')
            ];

        $this->conditionStatus = true;
    }
0 likes
2 replies

Please or to participate in this conversation.