3

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.

Tyssen
  • 1,569
  • 16
  • 35
  • Have you tried `curl_error($query)`? Either that, or making the request using [Paw](https://paw.cloud/), or the like? – Stephen Lewis Oct 21 '16 at 00:33
  • If I try Ex#1 from [here](http://php.net/manual/en/function.curl-error.php) I get _Operation completed without any errors_ but when I `var_dump(json_decode($resp))` I get `NULL` :? I had a look at Paw but couldn't figure out what value to enter for authentication. – Tyssen Oct 21 '16 at 04:35

1 Answers1

2

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);
Charles Ouellet
  • 6,338
  • 3
  • 41
  • 57
  • Thanks, just before coming back to this I actually came up with something quite close to that with help from Maxime on support. ;) – Tyssen Oct 21 '16 at 06:33