Example #1
0
function shopify_api_client($shops_myshopify_domain, $shops_token, $api_key, $secret, $private_app = false)
{
    $password = $private_app ? $secret : md5($secret . $shops_token);
    $baseurl = "https://{$shops_myshopify_domain}/";
    return function ($method, $path, $params = array(), &$response_headers = array()) use($baseurl, $password) {
        $url = $baseurl . ltrim($path, '/');
        $query = in_array($method, array('GET', 'DELETE')) ? $params : array();
        $payload = in_array($method, array('POST', 'PUT')) ? stripslashes(json_encode($params)) : array();
        $request_headers = in_array($method, array('POST', 'PUT')) ? array("Content-Type: application/json; charset=utf-8") : array();
        $request_headers['X-Shopify-Access-Token'] = $password;
        $response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
        $response = json_decode($response, true);
        if (isset($response['errors']) or $response_headers['http_status_code'] >= 400) {
            throw new ShopifyApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));
        }
        return (is_array($response) and count($response) > 0) ? array_shift($response) : $response;
    };
}
function insales_api_client($method, $path, $params = array(), $type = 'json', &$response_headers = array())
{
    $url = BASEURL_INSALES . ltrim($path, '/');
    $payload = in_array($method, array('POST', 'PUT')) ? $type == 'json' ? json_encode($params) : $params : ($type == 'json' ? array() : '');
    $request_headers = in_array($method, array('POST', 'PUT')) ? array("Content-Type: application/" . $type . "; charset=utf-8", 'Expect:') : array();
    $response = curl_http_api_request_($method, $url, $payload, $request_headers, $response_headers);
    switch ($type) {
        case 'json':
            $response = json_decode($response, true);
            if (isset($response['errors']) or $response_headers['http_status_code'] >= 400) {
                throw new InsalesApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'my_insales_domain'));
            }
            break;
        case 'xml':
            $response = simplexml_load_string($response);
            break;
    }
    return $response;
}
Example #3
0
function orderbox_api_client($userid, $password)
{
    $auth = array('auth-userid' => $userid, 'auth-password' => $password);
    return function ($method, $path, $params = array(), &$response_headers = array()) use($auth) {
        $url = ORDERBOX_HTTP_API_BASEURL . ltrim($path, '/');
        $query_params = empty($params) ? $auth : array_merge($params, $auth);
        $query = orderbox_http_build_query($query_params);
        if ('POST' == $method) {
            $payload = empty($params) ? '' : orderbox_http_build_query($params);
        } else {
            $payload = '';
        }
        $request_headers = 'POST' == $method ? array("Content-Type: application/x-www-form-urlencoded; charset=utf-8") : array();
        $response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
        $response = json_decode($response, true);
        if (isset($response['error']) or $response_headers['http_status_code'] >= 400) {
            throw new OrderboxApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'auth'));
        }
        return $response;
    };
}