403 Error on Plugin page with file_get_contents

Hi,

I developed a Kirby Plugin that worked fine for a couple of years since recently I’ve began to get a 403 error on a JSON file in the media folder that Kirby has created.
This error appears on a page that does a file_get_contents and loads the JSON file. But it is strange because if I type the URL of the file I have no problems at all, and also sometimes it loads it and sometimes it shows the 403 error… Any ideas how I’d approach this problem?

Thanks!

failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

Using file_get_contents to fetch stuff from remote locations is often not very stable. You could try to replace this with Remote::get() Remote::get() | Kirby CMS

Thank you! I replaced it with Remote::get() I added these lines that actually prevent the script to crash totally when gets a 403 error, but still I get 80% of the times a 403 code…

if ($response->code() === 200): $countries_list = $response->json(); else: $countries_list = []; endif;

Any Idea?

HTTP 403 is an error code that is set by the server you request the file from. It could be that they detect automated requests and block them.

What you could do is to store a successful 200 response in Kirby’s cache. This will make failed requests much less likely and will also significantly improve performance.

Very interesting, I’ve never used Kirby’s cache… I’ll dig into it.

It looks like the problem is generated by the request of a global url making a get_contents call on $site->url(). Is there a way to make it on a local path? I tried using $kirby->root(‘site’) but returns a wrong path