Example #1
0
function _api($method, $url, $query = '', $payload = '', $request_headers = array(), &$response_headers = array())
{
    try {
        $response = wcurl($method, $url, $query, $payload, $request_headers, $response_headers);
    } catch (WcurlException $e) {
        throw new CurlException($e->getMessage(), $e->getCode());
    }
    $response = json_decode($response, true);
    if (isset($response['errors']) or $response_headers['http_status_code'] >= 400) {
        throw new ApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));
    }
    return (is_array($response) and !empty($response)) ? array_shift($response) : $response;
}
Example #2
0
function _api($method, $url, $query = '', $payload = '', $request_headers = array(), &$response_headers = array(), $loop = 0)
{
    try {
        $response = wcurl($method, $url, $query, $payload, $request_headers, $response_headers);
    } catch (WcurlException $e) {
        throw new CurlException($e->getMessage(), $e->getCode());
    }
    $response = json_decode($response, true);
    if (isset($response['errors']) or $response_headers['http_status_code'] >= 400) {
        if ($loop < 3 && ($response_headers['http_status_code'] == 429 || calls_left($response_headers) == 0)) {
            usleep(500000);
            // sleep 0.5 second and try again (max 3 times)
            $loop++;
            return _api($method, $url, $query, $payload, $request_headers, $response_headers, $loop);
        }
        throw new ApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));
    }
    if (calls_left($response_headers) > 0 && calls_left($response_headers) <= 3) {
        usleep(100000);
    }
    return (is_array($response) and !empty($response)) ? array_shift($response) : $response;
}