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",
}
2
Answers
First of all the issue was coudn't resolved host which wasn't primary issue though. But after getting comments from this post i realized why that happened. Then the primaty 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
If you change
to
Then it might work.. This is because the URL you are passing is being "read" as
http://api/test
and the hostname/domainapi
doesn’t resolve to anything.However, if you are wanting to access an endpoint that exists within your application you can skip the whole HTTP setup process and directly access the method (Access Controller method from another controller in Laravel 5):
But ultimately, I would recommend moving code that you are going to reuse into a shared library.