Show the code that isn't working
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
@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 Yeah I figured. No problem :)
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 So 127.0.0.1:8080 is another site? What url is this one running on? Be aware that you have a site call itself if using php artisan serve
And if you want to make life a little easier, I can suggest you use the built in http client in laravel: https://laravel.com/docs/9.x/http-client#main-content
@Sinnbeck Hi sir, 127.0.0.1:8080 is my local host i already use the built in http client in laravel
@nbaluyot15 So the app is making a request to itself? That isnt possible with php artisan serve
@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']);
htt://jsonplaceholder.typicode.com/posts i replace my localhost api to this one, ''htt://jsonplaceholder.typicode.com/posts'
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
front end and back end API are in the same project and same localhost htt://127.0.0.1:8080/
i also use xampp apps in my API
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 ]
format your code blocks
you said you use Http client but then use guzzle?
what web server are you using?
I use xampp as my web server in API
@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.
Still can't get the API in my localhost using controller
@nbaluyot15 you can format your code using markdown, please see this link: https://www.freecodecamp.org/news/how-to-format-code-in-markdown/
Please or to participate in this conversation.