'Laravel Http::get() method not retrieving data from external API
I'm trying to fetch data from an external API with laravel. This is how I do it :
use Illuminate\Support\Facades\Http;
[..]
$response = Http::get('http://localhost:3000/api/movies');
dd($response->json()); // it's null
dd($response->collect()); // it's null
dd($response->object()); // it's null
//for debugging purpose
$response->ok() // this output true
$response->successful() //this output true
$response->failed() : bool; //this output false
$response->serverError() : bool; //this output false
$response->clientError() : bool; //this output false
The request seems to work i do receive a response but without data, the file i'm trying to read from is as follow:
[
{
"title": "ea officiis ducimus",
"year": 2029,
"poster": "http://placeimg.com/640/480"
},
{
"title": "ea laudantium cum",
"year": 2016,
"poster": "http://placeimg.com/640/480"
}
]
By the way it's not a json i think it's just a simple file that contains this it doesn't echo with php json data.
Also this is what i get when i go to the link in the browser: https://gyazo.com/fafd0e9f74a47785d48f360c7c7b3f33
UPDATE 1:this is what i get when i do this:
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://localhost:3000/api/movies');
dd($response->getBody());
GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to localhost port 3000: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://localhost:3000/api/movies
EDIT 2: I hosted the same file in another external server that isn't hosted on the same server in a different port and this time it worked.
Solution 1:[1]
Try below:
$response = Http::get('http://localhost:3000/api/movies');
$data = json_decode($response->body(), true);
dd($data);
Update:
As per the updated question and discussion, the problem is due to your laravel application is in homestead (vagrant) and the api is on localhost (outside the homestead). You can reach your host computer using IP 10.0.2.2 (if vagrant settings are default). Using this ip can resolve your problem.
Solution 2:[2]
I had a similar problem where the returns from my endpoints always returned empty,
After searching a bit, I found some parameters that helped me
$client = new Client();
$response = $client->get('http://localhost:3000/api/movies', [
'debug' => TRUE, // remove after test
'timeout' => 100,
'decode_content' => false,
'headers'=>[
'Content-Type' => 'application/json'
]
]);
$bodyResponse = $response->getBody();
$result = $bodyResponse->getContents();
$result = json_decode($result);
dd($result);
another alternative would be to create another instance of your local server in a second tab of the terminal like:
php artisan serve --port 3001
$client = new Client();
$response = $client->get('http://localhost:3001/api/movies', [
'debug' => TRUE, // remove after test
'timeout' => 100,
'decode_content' => false,
'headers'=>[
'Content-Type' => 'application/json'
]
]);
$bodyResponse = $response->getBody();
$result = $bodyResponse->getContents();
$result = json_decode($result);
dd($result);
so the api call is on 3001 and the view is on 3000, but this is only a solution for localhost, in production, this is not necessary
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jonnys J. |
