分类
后端

BigCommerce rest API

BigCommerce是一个基于SaaS的专业英文外贸电商独立站建站平台
官方网址 https://www.bigcommerce.com/
API文档地址 https://developer.bigcommerce.com/
在如下位置创建V2/V3 API

创建后 记录保存API密钥信息等

### 参考代码
class BigCommerceController
{
    private $token;
    private $client;
    private $store_hash;

    public function __construct()
    {
        $data = cmf_get_option('site_info');
        $this->token = $data['ACCESS_TOKEN'];
        $this->client = $data['CLIENT_SECRET'];
        $this->store_hash = $data['store_hash'];
        if (!$this->token || !$this->client || !$this->store_hash) {
            get_msg('配置文件错误');
            die('系统错误');
        }
    }

    public function get_curl($method, $url, $data = [])
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.bigcommerce.com/stores/" . $this->store_hash . "/v3/" . $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS => json_encode($data),
            CURLOPT_HTTPHEADER => array(
                "accept: application/json",
                "x-auth-client: " . $this->client,
                "x-auth-token: " . $this->token
            ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            get_msg($err);
            return false;
        } else {
            return json_decode($response, true);
        }
    }

    //获取商品列表
    public function get_product_list()
    {
        return $this->get_curl('GET', 'catalog/products?limit=10000');
    }

    //获取商品分类
    public function get_category_list()
    {
        return $this->get_curl('GET', 'catalog/categories?limit=10000');
    }

    //获取商品图片
    public function get_image_list($product_id)
    {
        return $this->get_curl('GET', 'catalog/products/' . $product_id . '/images');
    }

    //获取规格详情
    public function get_variants_list($product_id)
    {
        return $this->get_curl('GET', 'catalog/products/' . $product_id . '/variants');
    }
}