Chu Văn Hợp
Chu Văn Hợp

Reputation: 55

Laravel: file_get_contents not working

I am learning in Laravel and I got a strange error with Laravel 5.4 and XAMPP 5.6.23. Let say I have a Laravel app with APIController and UserController. In APIController.index() function:

public function index()
{
    $users = User::all();
    return response()->json($users);
}

In UserController.index() function:

public function index()
{
    $url = route("api.index");
    $content = @file_get_contents($url);
    dd($content);
}

Note route("api.index") return "http://localhost:8000/api", and is mapping to the APIController.index() function. In php.ini I have "allow_url_fopen" = On. The problem is file_get_contents() always exceed maximum time. When I replace $url = "https://jsonplaceholder.typicode.com/comments", however, it works. I dont know what's wrong cuz Laravel doesnt show me any error rather than Maximum time exceeded. Any one can help? Thank you!

Upvotes: 1

Views: 6686

Answers (2)

bigM
bigM

Reputation: 1

You can access http://localhost:8000/ from your pc (outside of XAMPP).
But, you need to check what happens to access http://localhost:8000/ from inside of XAMPP where PHP is running.

  1. Login to XAMPP server
  2. Try $ curl -i http://localhost:8000

My guess:

  1. :8000 is not open.
  2. You may need to change domain or port or path.

Upvotes: 0

Chu Văn Hợp
Chu Văn Hợp

Reputation: 55

After google for many times, my answer for this is : I cant read API data that come from the same project as where the controller come. I must write the function read API in jquery (in view not controller). Sadly,tho

Upvotes: 2

Related Questions