Tyssen Tyssen - 1 month ago 9
PHP Question

Custom Snipcart orders dashboard

I'm trying to create a custom Snipcart orders dashboard using their orders API but starting with this:

$query = curl_init();
$key = 'My-API-key';
$options = array(
CURLOPT_URL => 'https://app.snipcart.com/api/orders/',
CURLOPT_USERPWD => $key,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTPHEADER => 'Accept: application/json'
);

curl_setopt_array($query, $options);
$resp = curl_exec($query);
curl_close($query);
$body = json_decode($resp);


I'm not getting any output from
$resp
. Not sure where I'm going wrong.

Answer

We recently integrated Snipcart's data into a Wordpress site admin panel.

We use this code to make the request:

function call_snipcart_api($url, $method = "GET", $post_data = null) {
    $url = 'https://app.snipcart.com/api' . $url;

    $query = curl_init();

    $headers = array();
    $headers[] = 'Content-type: application/json';
    if ($post_data)
        $headers[] = 'Content-Length: ' . strlen($post_data);
    $headers[] = 'Accept: application/json';

    $secret = 'Secret API Key';
    $headers[] = 'Authorization: Basic '.base64_encode($secret . ":");
    $options = array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0
    );

    if ($post_data) {
        $options[CURLOPT_CUSTOMREQUEST] = $method;
        $options[CURLOPT_POSTFIELDS] = $post_data;
    }

    curl_setopt_array($query, $options);
    $resp = curl_exec($query);
    curl_close($query);

    return json_decode($resp);
}

We then use like like this:

// Get list of orders
$orders = call_snipcart_api('/orders');

// Get an order by its token
$order = call_snipcart_api('/orders/' . $orderToken);