Last active
July 30, 2018 01:05
-
-
Save summersab/366694873ccd786d1cd6461f707846d6 to your computer and use it in GitHub Desktop.
Queuing function for Order Desk API calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "order-desk-api-client.php"; | |
$executionStartTime = microtime(true); | |
register_shutdown_function("unlock"); | |
$od = new OrderDesk($storeid, $apikey); | |
$storeid = $_SERVER['HTTP_ORDERDESK_STORE_ID']; | |
$apikey = $_SERVER['HTTP_ORDERDESK_API_KEY']; | |
$url = substr($_SERVER['PATH_INFO'],1); | |
$method = strtolower($_SERVER['REQUEST_METHOD']); | |
$post = array(); | |
if ($method == "get") { | |
parse_str($_SERVER['QUERY_STRING'], $post); | |
$response = $od->$method($url, $post); | |
$response['execution_time'] = $executionStartTime; | |
echo print_r($response, 1); | |
exit; | |
} | |
else { | |
$post = json_decode($HTTP_RAW_POST_DATA, 1); | |
} | |
$od = new OrderDeskApiClient($storeid, $apikey); | |
if (@mkdir('/tmp/.'.$storeid, 0700)) { | |
$response = $od->$method($url, $post); | |
} | |
else { | |
for ($i = 1; 1; $i++) { | |
sleep(5 * rand(0, pow(2,$i) - 1)); | |
if (@mkdir('/tmp/.'.$storeid, 0700)) { | |
$response = $od->$method($url, $post); | |
break; | |
} | |
} | |
} | |
$response['execution_time'] = $executionStartTime; | |
echo print_r($response, 1); | |
function unlock() { | |
rmdir('/tmp/.'.$storeid); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment