1 Answer
  • 45
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /var/www/html/cnasolution/site/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/site/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/site/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Json API Rate Limiting

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

Download script demo [LINK] [Origin]
Download script demo [LINK 2] [Onedrive] Download script demo [LINK 2] [Google drive]

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); 
see demo
  • 28
Reply Report