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

Patwan's avatar

Prevent data from being cached after a loop in PHP

Hello all,,Am working with some PHP code whereby am reaching out to a payment gateway several times to check the status whether the user has paid. It is working fine except that some data is being cached by the loop,,, but I want to flush the data after each loop.. Am trying to use clearstatcache which aint working

~ Please assist?

//Loop through the code 3 times
        for($try=1; $try<=3; $try++) {
            sleep(15);
            $payStat = $this->global_Curl($data, 'api/payment/status')->data;
            var_dump($payStat);
            dd($payStat);
            //Avoids cache of the data
            clearstatcache();
        }
0 likes
6 replies
lostdreamer_nl's avatar

"It is working fine except that some data is being cached by the loop"

What is it that is being cached here? The only thing I can think of is that your request is being cached? (so after a while, the payment status is actually changed, but it still gets the old cached data?)

http://php.net/manual/en/function.clearstatcache.php

clearstatcache is not for that ;)

You could use the curl setting:

curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);

to force using a new connection every time

You could also add a unique GET parameter to the URI:

$payStat = $this->global_Curl($data, 'api/payment/status?cachebuster='. microtime(true))->data;
Patwan's avatar

@lostdreamer_nl Thanks for help,, Actually I have that line of code in my curl function. Have added your 2nd line of code and restarted the server but still not working..

Patwan's avatar

@lostdreamer_nl Actually I have found the bug,, the payment gateway is throwing a 500 internal server error and according to the docs if the status is 500,, I should continue hitting it until there is success... So I have modified my curl code that is being used to hit the API and added HTTP code as shown below:

public function global_Curl($data, $url)
    {
        //dd($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //Prevents usage of a cached version of the URL
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
        //Listen for HTTP errors
        curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        // Log::debug($response);
        return $response;
    }

Next in the for loop am looking for a way to listen if the status is 500 then continue looping

lostdreamer_nl's avatar
Level 53

Id put it in the global_Curl function:

public function global_Curl($data, $url, $try = 1)
    {
        //dd($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //Prevents usage of a cached version of the URL
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
        //Listen for HTTP errors
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        $response = json_decode(curl_exec($ch));
        curl_close($ch);

        if($statusCode == 500 && $try < 5) {
                // on HTTP status 500, we'll try up to 5x (dont want any eternal loop)
                // let's also wait for a second before trying again
                sleep(1);
                return $this->global_Curl($data, $url, ++$try);
        }
        
        return $response;
    }

Now you dont have to change your calling code and will know that, on 500 errors it will try again.....

Patwan's avatar

@lostdreamer_nl Just one last question,, when calling the global_Curl function you created the parameters dont match,, when calling it should I add the number of times I want in the loop as the last parameter

 for($try=1; $try<=3; $try++) {
            sleep(15);
            $payStat = $this->global_Curl($data, 'api/payment/status', 3)->data;
            var_dump($payStat);
            dd($payStat);
            //Avoids cache of the data
            clearstatcache();
        }
lostdreamer_nl's avatar

no actually, as you can see here:

public function global_Curl($data, $url, $try = 1)

It has a default setting.

So if you call it without the last parameter, it will count as the first try. If it recieves a 500 error, it will add 1 to the $try and try call itself again.

Until $try is 5, than it will stop.

So you should not use the third parameter of this method, it will handle itself.

Please or to participate in this conversation.