@@ -68,7 +68,12 @@ class ShopifySDK
6868 /**
6969 * @var float microtime of last api call
7070 */
71- public static $ microtimeOfLastAPICall ;
71+ public static $ microtimeOfLastApiCall ;
72+
73+ /**
74+ * @var float Minimum gap in seconds to maintain between 2 api calls
75+ */
76+ public static $ timeAllowedForEachApiCall = .5 ;
7277
7378 /**
7479 * Shop / API configurations
@@ -216,6 +221,10 @@ public static function config($config)
216221 self ::setAdminUrl ();
217222 }
218223
224+ if (isset ($ config ['AllowedTimePerCall ' ])) {
225+ static ::$ timeAllowedForEachApiCall = $ config ['AllowedTimePerCall ' ];
226+ }
227+
219228 return new ShopifySDK ;
220229 }
221230
@@ -256,26 +265,33 @@ public static function getAdminUrl() {
256265 /**
257266 * Maintain maximum 2 calls per second to the API
258267 *
268+ * @see https://help.shopify.com/api/guides/api-call-limit
269+ *
259270 * @param bool $firstCallWait Whether to maintain the wait time even if it is the first API call
260271 */
261272 public static function checkApiCallLimit ($ firstCallWait = false )
262273 {
263- if (static ::$ microtimeOfLastAPICall == null ) {
274+ $ timeToWait = 0 ;
275+ if (static ::$ microtimeOfLastApiCall == null ) {
264276 if ($ firstCallWait ) {
265- usleep ( 500000 ) ;
277+ $ timeToWait = static :: $ timeAllowedForEachApiCall ;
266278 }
267279 } else {
268280 $ now = microtime (true );
269- $ timeSinceLastCall = $ now - static ::$ microtimeOfLastAPICall ;
281+ $ timeSinceLastCall = $ now - static ::$ microtimeOfLastApiCall ;
270282 //Ensure 2 API calls per second
271- if ($ timeSinceLastCall < .5 ) {
272- $ timeToWait = .5 - $ timeSinceLastCall ;
273- //convert time to microseconds
274- $ microSecondsToWait = $ timeToWait * 1000000 ;
275- //Wait to maintain the API call difference of .5 seconds
276- usleep ($ microSecondsToWait );
283+ if ($ timeSinceLastCall < static ::$ timeAllowedForEachApiCall ) {
284+ $ timeToWait = static ::$ timeAllowedForEachApiCall - $ timeSinceLastCall ;
277285 }
278286 }
279- static ::$ microtimeOfLastAPICall = microtime (true );
287+
288+ if ($ timeToWait ) {
289+ //convert time to microseconds
290+ $ microSecondsToWait = $ timeToWait * 1000000 ;
291+ //Wait to maintain the API call difference of .5 seconds
292+ usleep ($ microSecondsToWait );
293+ }
294+
295+ static ::$ microtimeOfLastApiCall = microtime (true );
280296 }
281297}
0 commit comments