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

nbaluyot15's avatar

How to connect localhost api url on laravel 8 controller?

i can get result using postman but can't get the result data in controller

0 likes
18 replies
nbaluyot15's avatar

@Sinnbeck the code inclide http before the api linl, i can't include it here because it said that its my first day in this forum

nbaluyot15's avatar

public function index() { $client = new \GuzzleHttp\Client(); $request = $client->get('://127.0.0.1:8080/api/authors/get'); $response = $request->getBody()->getContents(); echo '

';
print_r($response);
exit;
}

nbaluyot15's avatar

@Sinnbeck Hi sir, 127.0.0.1:8080 is my local host i already use the built in http client in laravel

Sinnbeck's avatar

@nbaluyot15 So the app is making a request to itself? That isnt possible with php artisan serve

1 like
nbaluyot15's avatar

@Sinnbeck yesss

this is the code in my PagesController.php public function index() { $client = new \GuzzleHttp\Client(); $request = $client->get('://127.0.0.1:8080/api/authors/get'); $response = $request->getBody()->getContents(); return $response; }

this is my code in api.php Route::get('/authors/get', [AuthorController::class, 'index']);

nbaluyot15's avatar

htt://jsonplaceholder.typicode.com/posts i replace my localhost api to this one, ''htt://jsonplaceholder.typicode.com/posts'

nbaluyot15's avatar

when i try to get the data from this site sample data of json, i got the result. but if i get the data fomr my localhosts the page is loading only

nbaluyot15's avatar

front end and back end API are in the same project and same localhost htt://127.0.0.1:8080/

nbaluyot15's avatar

i try this in mycontroller and this is the resul i got public function index() { return Http::dd()->get('htt://127.0.0.1:8080/api/authors/get'); }

Illuminate\Http\Client\Request {#357 ▼ #request: GuzzleHttp\Psr7\Request {#345 ▼ -method: "GET" -requestTarget: null -uri: GuzzleHttp\Psr7\Uri {#340 ▼ -scheme: "http" -userInfo: "" -host: "127.0.0.1" -port: 8080 -path: "/api/authors/get" -query: "" -fragment: "" -composedComponents: null } -headers: array:2 [▼ "User-Agent" => array:1 [▶] "Host" => array:1 [▶] ] -headerNames: array:2 [▼ "user-agent" => "User-Agent" "host" => "Host" ] -protocol: "1.1" -stream: GuzzleHttp\Psr7\Stream {#344 ▼ -stream: stream resource @491 ▶} -size: 0 -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] } } #data: [] }

array:10 [▼ "http_errors" => false "laravel_data" => [] "on_stats" => Closure($transferStats) {#341 ▶} "synchronous" => true "handler" => GuzzleHttp\HandlerStack {#323 ▶} "cookies" => GuzzleHttp\Cookie\CookieJar {#339 ▶} "allow_redirects" => array:5 [▶] "decode_content" => true "verify" => true "idn_conversion" => false ]

Snapey's avatar

format your code blocks

you said you use Http client but then use guzzle?

what web server are you using?

Sinnbeck's avatar

@nbaluyot15 So xampp running on 127.0.0.1:8080

What is the point of calling the site itself as an API? I have never heard of a reason to do that.

nbaluyot15's avatar

Still can't get the API in my localhost using controller

Please or to participate in this conversation.