Why my API call works on my local server and not online? (error 500)

I have two different website (two different domains) which use laravel.
One of them (which I’ll call here api.com) provides an api with different routes and is actually on a local server (111.111.1.111) :

routes/api.php :

Route::prefix('news')->group(function () {

    Route::get('', [ApiController::class, 'newsIndex']);
    Route::get('{id}', [ApiController::class, 'newsShow']);
});

ApiController :

    public function newsIndex()
    {
        $news = News::orderByDesc('ordre')
                    ->where('site_destination', 'like', '%other%')
                    ->where('statut', '=', 1)
                    ->get();

        return response()->json($news);
    }

    public function newsShow($id) 
    {
        $news = News::findOrfail($id);

        return response()->json($news);
    }

I have to call these APIs from my second website (which I will call here request.com).
I want to deploy this one online (planethoster server). I succeed to deploy it but my page where I called API from api.com doesn’t work : return 500 error.

Into my controller on request.com :

    public function index() 
    {
        $newsListFromApi = json_decode(file_get_contents("http://111.111.1.111/api/news"));
        $newsFirstPictureList = [];
        foreach ($newsListFromApi as $key => $value) {
            $newsFirstPictureList[$value->id] = json_decode(file_get_contents("http://111.111.1.111/api/news/" . $value->id . "/firstPicture"));
        }
    
        return View::make('client.news.index', [ 
            'newsListFromApi' => $newsListFromApi,
            'newsFirstPictureList' => $newsFirstPictureList,
         ]);
    }

    public function show($newsId)
    {
        $news = json_decode(file_get_contents("http://111.111.111/api/news/" . $newsId));
        $newsDocs = json_decode(file_get_contents("http://111.111.1.111/api/news/" . $newsId . "/docs"));
        // dump($newsDocs);
        return View::make('client.news.show', [
            'news' => $news,
            'newsDocs' => $newsDocs,
        ]);
    }

If request.com is on localhost (production or development mode) it works fine. I only have warning message in console about mixed contents that I’m aware of. But if I deploy request.com I have a 500 error.

On the logs I have this entry :

*[2022-06-13 13:55:03] production.ERROR: file_get_contents(http://111.111.1.111/api/news): failed to open stream: Connection timed out {"userId":x,"email":"xx","exception":"[object] (ErrorException(code: 0): file_get_contents(http://111.111.1.111/api/news): failed to open stream: Connection timed out at /home/xx/laravel/releases/20220613-120400/app/Http/Controllers/Client/NewsController.php:12)
[stacktrace]

The api.com et request.com api aren’t on the same server on development.

Do you think that it can be caused by the mixed contents error? I think that the browser will only not display images but display the page anyway…

Also I think it can be a problem of cross origin but I hadn’t such an error message of it.

I suppose, thanks to the log, that it has a problem with this line of code when I call the API :

$newsListFromApi = json_decode(file_get_contents("http://111.111.1.111/api/news"));

I am used to call API on JS with fetch(Ajax) but not with PHP…

Why does it work locally, but not online?

>Solution :

You can connect to your local server from outside of your network only if it has public access (open TCP/IP ports and public IP). http://111.111.1.111 is a local IP address in this context.

You could deploy your API service to another server, and then the two would be able to connect.

Leave a Reply