我尝试使用orders API创建自定义Snipcart订单信息中心,但从此开始:
$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);
我没有从$resp
获得任何输出。不知道我哪里出错了。
答案 0 :(得分:2)
我们最近将Snipcart的数据集成到Wordpress网站管理面板中。
我们使用此代码发出请求:
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);
}
然后我们像这样使用:
// Get list of orders
$orders = call_snipcart_api('/orders');
// Get an order by its token
$order = call_snipcart_api('/orders/' . $orderToken);