Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Wojtek-R's avatar

Caching Guzzle HTTP Client response and displaying it in Json

I am getting and error whenever I try to display a cached response in json().

error message:

fseek(): Argument #1 ($stream) must be of type resource, int given

my code:

$response = cache()->remember('Nav', 54000, function (){
                return Http::get($url);
            });

 dd($response->json()); 

I am storing my cache in file.

0 likes
13 replies
tykus's avatar

Why are you caching the Response instance, and not the result?

$response = cache()->remember('Nav', 54000, function (){
    $response = Http::get($url);
    return $response->json(); 
});
Wojtek-R's avatar

Still getting the same error while trying to dd($response->json) - thats the format that I need to get it in.

Wojtek-R's avatar

@tykus it is basically url to Json file which is on the web, calling $response->json works fine without caching.

tykus's avatar

@Wojtek-R it should work as expected; what is your setup - cache driver, environment?

1 like
Wojtek-R's avatar

@tykus Thanks for help, i have managed to get it to work with this code:

$nav = Cache::remember('nav', 15, function () {
                return Http::get($url)->json();
            });
tykus's avatar

@Wojtek-R how is that different from what I posted earlier (inlining the json method call aside)? Also, make sure you're passing $url into the Closure scope

Wojtek-R's avatar

@tykus In my case I am guessing it was a matter of:

cache()->remember(

Instead of:

Cache::remember(
Wojtek-R's avatar

@tykus that is literally all I've changed, $url is just to show what I am trying to do, in my code it is just a hardcoded url string (not a passed in variable).

webrobert's avatar

just a heads up using this strategy. There maybe cases where the result of the request is incorrect/incomplete in some way BUT doesn't break AND then the cache could be unwanted. In those cases have a way to clear the cache. For example, I have service that sometimes returns empty results but all status codes are valid.

1 like
Wojtek-R's avatar

@webrobert good insight, thanks, is there some simple way to check if the request data is incomplete? The json data that I am getting is variable - it changes.

webrobert's avatar

@Wojtek-R like I said, I've had apis that just return empty. So you're fooled into thinking that there aren't results. It's one of those silly things that you don't realize until your troubleshooting why you have a whole string of failed/incomplete somethings. It all depends on the api provider. Just be mindful that however that request comes back is how it is cached. Your system has to be smart enough (your logic) to dump the cache if it thinks there is an issue. And it could all be mute for what you are building. It's just one of those clever pieces of code that merits a warning.

1 like

Please or to participate in this conversation.