• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Connect PHP applications with the Bigcommerce Platform

Bigcommerce API Client

PHP client for connecting to the Bigcommerce V2 REST API.

To find out more, visit the official documentation website: https://developer.bigcommerce.com/

Latest Stable Version Total Downloads

Requirements

  • PHP 7.1 or greater
  • curl extension enabled

To generate an OAuth API token, follow this guide.

To connect to the API with OAuth you will need the following:

  • client_id
  • auth_token
  • store_hash

Installation

Use the following Composer command to install the API client from the Bigcommerce vendor on Packagist:

 $ composer require bigcommerce/api
 $ composer update

You can also install composer for your specific project by running the following in the library folder.

 $ curl -sS https://getcomposer.org/installer | php
 $ php composer.phar install
 $ composer install

Namespace

All the examples below assume the Bigcommerce\Api\Client class is imported into the scope with the following namespace declaration:

use Bigcommerce\Api\Client as Bigcommerce;

Configuration

To use the API client in your PHP code, ensure that you can access Bigcommerce\Api in your autoload path (using Composer’s vendor/autoload.php hook is recommended).

Provide your credentials to the static configuration hook to prepare the API client for connecting to a store on the Bigcommerce platform:

OAuth

In order to obtain the auth_token you would consume Bigcommerce::getAuthToken method during an installation of a single-click app. Alternatively, if you already have a token, skip to Bigcommerce::configure and provide your credentials.

$object = new \stdClass();
$object->client_id = 'xxxxxx';
$object->client_secret = 'xxxxx';
$object->redirect_uri = 'https://app.com/redirect';
$object->code = $request->get('code');
$object->context = $request->get('context');
$object->scope = $request->get('scope');

$authTokenResponse = Bigcommerce::getAuthToken($object);

Bigcommerce::configure([
    'client_id' => 'xxxxxxxx',
    'auth_token' => $authTokenResponse->access_token,
    'store_hash' => 'xxxxxxx'
]);

Basic Auth (deprecated)

Bigcommerce::configure([
	'store_url' => 'https://store.mybigcommerce.com',
	'username'	=> 'admin',
	'api_key'	=> 'd81aada4xc34xx3e18f0xxxx7f36ca'
]);

Connecting to the store

To test that your configuration was correct and you can successfully connect to the store, ping the getTime method which will return a DateTime object representing the current timestamp of the store if successful or false if unsuccessful:

$ping = Bigcommerce::getTime();

if ($ping) echo $ping->format('H:i:s');

Accessing collections and resources (GET)

To list all the resources in a collection:

$products = Bigcommerce::getProducts();

foreach ($products as $product) {
	echo $product->name;
	echo $product->price;
}

To access a single resource and its connected sub-resources:

$product = Bigcommerce::getProduct(11);

echo $product->name;
echo $product->price;

To view the total count of resources in a collection:

$count = Bigcommerce::getProductsCount();

echo $count;

Paging and Filtering

All the default collection methods support paging, by passing the page number to the method as an integer:

$products = Bigcommerce::getProducts(3);

If you require more specific numbering and paging, you can explicitly specify a limit parameter:

$filter = ['page' => 3, 'limit' => 30];

$products = Bigcommerce::getProducts($filter);

To filter a collection, you can also pass parameters to filter by as key-value pairs:

$filter = ['is_featured' => true];

$featured = Bigcommerce::getProducts($filter);

See the API documentation for each resource for a list of supported filter parameters.

Updating existing resources (PUT)

To update a single resource:

$product = Bigcommerce::getProduct(11);

$product->name = 'MacBook Air';
$product->price = 99.95;
$product->update();

You can also update a resource by passing an array or stdClass object of fields you want to change to the global update method:

$fields = [
	'name'  => 'MacBook Air',
	'price' => 999.95
];

Bigcommerce::updateProduct(11, $fields);

Creating new resources (POST)

Some resources support creation of new items by posting to the collection. This can be done by passing an array or stdClass object representing the new resource to the global create method:

$fields = [
	'name' => 'Apple'
];

Bigcommerce::createBrand($fields);

You can also create a resource by making a new instance of the resource class and calling the create method once you have set the fields you want to save:

$brand = new Bigcommerce\Api\Resources\Brand();

$brand->name = 'Apple';
$brand->create();

Deleting resources and collections (DELETE)

To delete a single resource you can call the delete method on the resource object:

$category = Bigcommerce::getCategory(22);
$category->delete();

You can also delete resources by calling the global wrapper method:

Bigcommerce::deleteCategory(22);

Some resources support deletion of the entire collection. You can use the deleteAll methods to do this:

Bigcommerce::deleteAllOptionSets();

Using The XML API

By default, the API client handles requests and responses by converting between JSON strings and their PHP object representations. If you need to work with XML you can switch the API into XML mode with the useXml method:

Bigcommerce::useXml();

This will configure the API client to use XML for all subsequent requests. Note that the client does not convert XML to PHP objects. In XML mode, all object parameters to API create and update methods must be passed as strings containing valid XML, and all responses from collection and resource methods (including the ping, and count methods) will return XML strings instead of PHP objects. An example transaction using XML would look like:

Bigcommerce::useXml();

$xml = '<?xml version="1.0" encoding="UTF-8"?>
		<brand>
		 	<name>Apple</name>
		 	<search_keywords>computers laptops</search_keywords>
		</brand>';

$result = Bigcommerce::createBrand($xml);

Handling Errors And Timeouts

For whatever reason, the HTTP requests at the heart of the API may not always succeed.

Every method will return false if an error occurred, and you should always check for this before acting on the results of the method call.

In some cases, you may also need to check the reason why the request failed. This would most often be when you tried to save some data that did not validate correctly.

$orders = Bigcommerce::getOrders();

if (!$orders) {
	$error = Bigcommerce::getLastError();
	echo $error->code;
	echo $error->message;
}

Returning false on errors, and using error objects to provide context is good for writing quick scripts but is not the most robust solution for larger and more long-term applications.

An alternative approach to error handling is to configure the API client to throw exceptions when errors occur. Bear in mind, that if you do this, you will need to catch and handle the exception in code yourself. The exception throwing behavior of the client is controlled using the failOnError method:

Bigcommerce::failOnError();

try {
	$orders = Bigcommerce::getOrders();

} catch(Bigcommerce\Api\Error $error) {
	echo $error->getCode();
	echo $error->getMessage();
}

The exceptions thrown are subclasses of Error, representing client errors and server errors. The API documentation for response codes contains a list of all the possible error conditions the client may encounter.

Verifying SSL certificates

By default, the client will attempt to verify the SSL certificate used by the Bigcommerce store. In cases where this is undesirable, or where an unsigned certificate is being used, you can turn off this behavior using the verifyPeer switch, which will disable certificate checking on all subsequent requests:

Bigcommerce::verifyPeer(false);

Connecting through a proxy server

In cases where you need to connect to the API through a proxy server, you may need to configure the client to recognize this. Provide the URL of the proxy server and (optionally) a port to the useProxy method:

Bigcommerce::useProxy('http://proxy.example.com', 81);

More Repositories

1

gruf

gRPC Ruby Framework
Ruby
561
star
2

cornerstone

The BigCommerce Cornerstone theme
HTML
282
star
3

sass-style-guide

Sass coding guidelines for BigCommerce themes
CSS
280
star
4

storefront-data-hooks

Hooks for React Storefront UI Components
TypeScript
166
star
5

checkout-sdk-js

BigCommerce Checkout JavaScript SDK
TypeScript
121
star
6

gatsby-bigcommerce-netlify-cms-starter

Example Gatsby, BigCommerce and Netlify CMS project meant to jump start JAMStack ecommerce sites.
CSS
118
star
7

bigcommerce-for-wordpress

A headless commerce integration for WordPress, powered by BigCommerce
PHP
106
star
8

checkout-js

Optimized One-Page Checkout
TypeScript
102
star
9

catalyst

Catalyst - for Composable Commerce
TypeScript
100
star
10

stencil-cli

BigCommerce Stencil emulator for local theme development
JavaScript
100
star
11

bigcommerce-api-python

Python client library for Bigcommerce API
Python
83
star
12

bigcommerce-api-ruby

Connect Ruby applications with the Bigcommerce Platform
Ruby
77
star
13

laravel-react-sample-app

Sample BigCommerce App Using Laravel and React
PHP
69
star
14

gruf-demo

A demonstration Rails application utilizing gruf, a gRPC Rails framework.
Ruby
57
star
15

bc-nuxt-vue-starter

A starter site for Vue + Nuxt based storefronts that uses Divante's Storefront UI and BC's GraphQL API
Vue
51
star
16

sample-app-nodejs

A reference implementation of a BigCommerce single-click app, in Node.JS + Next.js/React
TypeScript
43
star
17

big-design

Design system that powers the BigCommerce ecosystem.
TypeScript
42
star
18

dev-docs

This repo contains the markdown files and static assets powering developer.bigcommerce.com. https://developer.bigcommerce.com/
41
star
19

api-specs

OpenAPI Specifications, Swagger, and JSON schema used to generate the human-readable BigCommerce API Reference.
Python
34
star
20

stencil-utils

Utility library for the Stencil theme framework.
JavaScript
33
star
21

hello-world-app-python-flask

Hello World sample app in Python and Flask
Python
31
star
22

b2b-buyer-portal

B2B Buyer Portal - BigCommerce B2B Edition
TypeScript
25
star
23

grphp

PHP gRPC Framework
PHP
24
star
24

hello-world-app-php-silex

Hello World sample app in PHP and Silex
PHP
23
star
25

gruf-rspec

RSpec helper suite for gruf
Ruby
20
star
26

script-loader-js

A library for loading JavaScript files asynchronously
TypeScript
17
star
27

channels-app

TypeScript
13
star
28

omniauth-bigcommerce

OmniAuth Bigcommerce Strategy
Ruby
13
star
29

storefront-api-examples

Example of using the GraphQL Storefront API to power a static site using Bootstrap and VanillaJS
HTML
12
star
30

paper

Paper assembles templates and translations and renders pages using backend template engines
JavaScript
11
star
31

subscription-foundation

Foundation for building custom subscription applications w/ BigCommerce
TypeScript
11
star
32

net-http

A basic HTTP client.
PHP
11
star
33

ai-app-foundation

TypeScript
10
star
34

widget-builder

TypeScript
10
star
35

hello-world-app-ruby-sinatra

Hello World sample app with Ruby, Sinatra and DataMapper
Ruby
10
star
36

paper-handlebars

Paper plugin for rendering via Handlebars.js
JavaScript
9
star
37

docs

The open source docs home for BigCommerce, including API specifications in OAS YAML and narrative docs in MDX
MDX
9
star
38

gruf-circuit-breaker

Circuit breaker support for gruf
Ruby
8
star
39

bigcommerce-api-node

A node module for authentication and communication with the BigCommerce API
TypeScript
7
star
40

gruf-newrelic

New Relic tracing for gruf services
Ruby
7
star
41

point-of-sale-foundation

Foundation for building custom POS applications w/ BigCommerce
TypeScript
7
star
42

gruf-prometheus

Gruf plugin for Prometheus support
Ruby
5
star
43

gruf-profiler

Profiler for gruf-backed gRPC requests
Ruby
5
star
44

gruf-zipkin

Zipkin tracing plugin for Gruf
Ruby
5
star
45

gruf-commander

Command/Request library for Gruf request validation
Ruby
5
star
46

php-resque-pause

An addon for php-resque, php-resque-pause adds functionality to pause resque jobs.
PHP
5
star
47

data-store-js

A JavaScript library for managing application state
TypeScript
5
star
48

stencil-styles

Compiles SCSS for the Stencil Framework
HTML
5
star
49

form-poster-js

Post HTML form programmatically
TypeScript
5
star
50

netlify-nextjs-starter

TypeScript
4
star
51

mock-injector

Auto-mocking dependencies for DI components testing.
PHP
4
star
52

theme-context-object-schemas

JSON schema used to generate the human-readable BigCommerce Handlebars Reference.
4
star
53

gruf-lightstep

LightStep tracing for gruf
Ruby
4
star
54

validate-commits

Commit message validator
JavaScript
4
star
55

gruf-sentry

Sentry integration for gruf
Ruby
3
star
56

request-sender-js

HTTP request client for browsers
TypeScript
3
star
57

memoize-js

A JavaScript library for memoizing the result of a pure function
TypeScript
3
star
58

stencil-lang-validator

Validate language keys used in templates and scripts
JavaScript
3
star
59

statsd-client

Record timing, increment, and count metrics in StatsD
PHP
3
star
60

injector

Dependency Injector component built on top of Pimple container.
PHP
3
star
61

eslint-config

JavaScript
3
star
62

bigpay-client-js

Bigpay client-side library
JavaScript
3
star
63

stand-with-ukraine-backend

Stand With Ukraine is a BigCommerce application. It allows merchants to easily add a widget to their storefront with a customized message and list of Charities that the merchant would like their shoppers to visit and support.
Rust
2
star
64

bc-lightstep-ruby

Generic lightstep library for distributed tracing in ruby
Ruby
2
star
65

sample-shipping-provider

Silex based reference implementation of a Shipping Carrier Service integration
PHP
2
star
66

nomad-workload-cpu-actuals-report-generator

Groovy
2
star
67

grphp-statsd

StatsD interceptor for measuring grphp client requests.
PHP
2
star
68

tslint-config

Default TSLint configuration used at BigCommerce
2
star
69

noopraven-go

A raven-go interface with a noop implementation.
Go
2
star
70

ruby-rails-react-sample-app

BigCommerce App - Ruby on Rails + React + BigDesign
Ruby
2
star
71

nextjs-contentstack-starter

TypeScript
2
star
72

stand-with-ukraine-frontend

Stand With Ukraine is a BigCommerce application. It allows merchants to easily add a widget to their storefront with a customized message and list of Charities that the merchant would like their shoppers to visit and support.
TypeScript
2
star
73

threatDragon

This repo is for storing threat modelling of BigCommerce projects
1
star
74

gruf-balancer

Percentage-based balancing of gruf-client requests for testing
Ruby
1
star
75

bonvoy

Go utility CLI tool for Envoy and Consul Connect
Go
1
star
76

themes-lib-scroll-link

JavaScript
1
star
77

app-sdk-js

JavaScript
1
star
78

puppet-module-supervisor

Puppet module for configuring the supervisor daemon control utility
Ruby
1
star
79

themes-lib-compare

JavaScript
1
star
80

themes-lib-squid

JavaScript
1
star
81

drupal-module-clientside_validation

Fork of the Drupal clientside_validation module to fix an upstream Internet Explorer bug - http://drupal.org/node/1995314
1
star
82

data-docker-debian-runfromenv

Basic Debian image to run a user-supplied script from the environment.
Shell
1
star
83

optimized-checkout-changelog

Summarises the changes made to the Optimized One Page Checkout Angular application.
1
star
84

themes-lib-loading

JavaScript
1
star
85

themes-lib-swipe-fade

JavaScript
1
star
86

bc-prometheus-ruby

Drop-in support for prometheus metrics for Ruby apps
Ruby
1
star
87

themes-lib-modal

JavaScript
1
star
88

axfr2tf

Converts an AXFR DNS query to Terraform resources
Rust
1
star