Search code examples
phplaravelcurl

Api consumption for internal laravel api is getting failed -cURL error 28: Connection timeout


This is the error message I am getting, when hitting the url http://localhost:8000/test
cURL error 6: Could not resolve host: api (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for api/test

using full url Like this Http::get('http://localhost:8000/api/test')

get this error

cURL error 28: Connection timeout after x as well

Here is the api file for defining route for /api/test

api.php

Route::get('/test', function(){
    return response()->json([
        'name' => 'John Doe',
        'address' => 'USA'
    ]);
});

web.php

Route::get('/test', [WebPostController::class, 'info']);

WebPostController.php

class WebPostController extends Controller
{

    public function info()
    {
        $response = Http::get('/api/test');

        $data = $response->json();
        return $data;
    }

P.S: External api is working fine however internal api which has created inside the same laravel app is getting failed . The api endpoint is working fine in the POSTMAN.

Version - PHP 8.2.12 — Laravel 11.27.2

I want to fetch the data from api endpoint like this

{
  "name": "X",
  "address": "Y",
}

Solution

  • First of all, the issue "coudn't resolved host" which wasn't primary issue though. But after getting comments from this post i realized why that happened. Then the primary issue i got which was server getting me timed out after certain time.

    Well, the issue is the default server of php i am using for laravel which can handle only single request at a time so when i was making request through url it coudn't handle multiple request at a time (browser request + api request) so i have to switched apache server which solved the problem of the time out.

    #thanks