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

tsy0730's avatar

CURL request in Laravel not returning response

I'm a novice of Laravel. I'm trying to make a CURL request to another endpoint in my Laravel application, but when I click the search button, the browser keeps loading and doesn't show anything. I've tried adding dd($result) and dd($inventory) to my code to debug the issue, but nothing is being printed.

Here's the relevant code in my routes/api.php:

Route::get('/api/client/{category}', [InventoryApiController::class, 'searchByCategory']);

Here's the relevant code in my routes/web.php:

Route::get('/client', function (){
    return view('inventory.client');
});

Route::post('/client', [InventoryApiController::class, 'search']);

Here's the relevant code in my InventoryApiController.php file:

public function search(Request $request){
    $category = $request->input('category');
    $url = "http://127.0.0.1:8000/api/client/$category";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if ($result === false) {
        $error = curl_error($ch);
        curl_close($ch);
        return view('inventory.client')->with('error', $error);
    }
    curl_close($ch);
    $inventory = json_decode($result);
    return view('inventory.client', compact('inventory'));
}

Here's the relevant code in my client.blade.php file:

<form method="POST" action="{{ url('/client') }}">
    @csrf
    <input type="text" name="category" placeholder="Enter category">
    <button type="submit">Search</button>
</form>

@if(isset($inventory))
    <h2>Search Results:</h2>
    <ul>
        @foreach($inventory as $item)
            <li>{{ $item->name }} - {{ $item->description }} - {{ $item->image }} - {{ $item->quantity }} - {{ $item->price }}</li>
        @endforeach
    </ul>
@else
    <p>No results found.</p>
@endif

I'm not sure what's causing this issue or how to debug it further. Any help would be greatly appreciated. Thank you!

0 likes
0 replies

Please or to participate in this conversation.