1

example.com/series/title-series-A/ cached with wordpress plugin for 1 day, but when cache expired and visitor access post series

example visitor access url below at same time

  • example.com/series/title-series-A/
  • example.com/series/title-series-B/
  • example.com/series/title-series-C/

so all series getting access by visitor will request to api at same time, any solution for this ?

( in this all posttype 'series' has api php request )

function curl_get_contents($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
$agent  = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
        $api = 'https://api.example.com/v3/title-series-here';
        $results = curl_get_contents($api, false, stream_context_create($agent));
    $results = json_decode($results, true);

And api.example.com has Rate Limiting 4 sec each request, if more than that will blocked

Johan
  • 19
  • 5
  • Just add a sleep(5); after each request. The code will be "pause" for 5 seconds – Inazo Jul 15 '20 at 09:00
  • But will all post series request will pause for 5 seconds? or just api request? because posting a, b, c accessed at the same time, and pause for 5 seconds at same time? – Johan Jul 15 '20 at 09:07

2 Answers2

1

Yes you need to add the sleep in your curl_get_content() function :

    function curl_get_contents($url)
    {
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
    
        $data = curl_exec($ch);
        sleep(5); // here after the request will sleep 5 seconds to permit the rate limit
    
    curl_close($ch);
    
        return $data;
    }
    $agent  = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
            $api = 'https://api.example.com/v3/title-series-here';
            $results = curl_get_contents($api, false, stream_context_create($agent));
        $results = json_decode($results, true);
Inazo
  • 488
  • 4
  • 15
  • Thanks for your answer, but i think is impossible after test it in localhost, if i using sleep() so page load become long because sleep function. or maybe i wrong question hehe how i can permit the rate limit if 3 pages load api at same time ? – Johan Jul 15 '20 at 09:25
  • That's normal if you wan't that not have impact your loading you have to change when you call the API and how you call the API. Maybe with cron or other method. – Inazo Jul 15 '20 at 09:57
1

This is my solution:

  • Check response header api, if header response 429 use sleep(5); or die();
if(($httpcode == 429)) { sleep(5); }
  • cache curl result
Johan
  • 19
  • 5