Integration Code Samples
Do you want to integrate API2Cart with your shopping cart or web site? We suggest some open source code samples to help you get started. The use of PHP code samples will provide smooth and successful API2Cart experience. The page is intended to facilitate developers in creating online applications for eCommerce.
Note
Unless explicitly identified as such, the sample code here is not certified or supported by API2Cart; it is intended for educational or testing purposes only.
PHP Code Sample
-
lib
-
Entity.php
<?php include_once 'Api2Cart/Connector.php'; /** * Class for work with the cart's entities (product, category, etc.) */ class Entity { /** * @var Connector $_api Connector from API2Cart */ protected $_api; public function __construct($apiKey, $storeKey) { $this->_api = new Connector($apiKey, $storeKey); } }
-
Entities
-
Product.php
<?php class Product extends Entity { /** * Perform method product.list.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-list */ public function apiList($params) { return $this->_api->request('product.list', $params); } /** * Perform method product.count.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-count */ public function apiCount($params) { return $this->_api->request('product.count', $params); } /** * Perform method product.info.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-info */ public function apiInfo($params) { return $this->_api->request('product.info', $params); } /** * Perform method product.find.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-find */ public function apiFind($params) { return $this->_api->request('product.find', $params); } /** * Perform method product.fields.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-fields */ public function apiFields() { return $this->_api->request('product.fields'); } /** * Perform method product.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-add */ public function apiAdd($params) { return $this->_api->request('product.add', $params); } /** * Perform method product.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-update */ public function apiUpdate($params) { return $this->_api->request('product.update', $params); } /** * Perform method product.image.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-image-add */ public function apiImageAdd($params) { return $this->_api->request('product.image.add', $params); } /** * Perform method product.image.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-image-update */ public function apiImageUpdate($params) { return $this->_api->request('product.image.update', $params); } /** * Perform method product.image.delete.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-image-delete */ public function apiImageDelete($params) { return $this->_api->request('product.image.delete', $params); } /** * Perform method product.option.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-option-add */ public function apiOptionAdd($params) { return $this->_api->request('product.option.add', $params); } /** * Perform method product.option.assign.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-option-assign */ public function apiOptionAssign($params) { return $this->_api->request('product.option.assign', $params); } /** * Perform method product.option.value.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-option-value-add */ public function apiOptionValueAdd($params) { return $this->_api->request('product.option.value.add', $params); } /** * Perform method product.option.value.assign.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-option-value-assign */ public function apiOptionValueAssign($params) { return $this->_api->request('product.option.value.assign', $params); } /** * Perform method product.variant.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-variant-add */ public function apiVariantAdd($params) { return $this->_api->request('product.variant.add', $params); } /** * Perform method product.variant.list.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-variant-list */ public function apiVariantList($params) { return $this->_api->request('product.variant.list', $params); } /** * Perform method product.variant.count.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-variant-count */ public function apiVariantCount($params) { return $this->_api->request('product.variant.count', $params); } /** * Perform method product.variant.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-variant-update */ public function apiVariantUpdate($params) { return $this->_api->request('product.variant.update', $params); } /** * Perform method product.variant.delete.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-variant-delete */ public function apiVariantDelete($params) { return $this->_api->request('product.variant.delete', $params); } /** * Perform method product.tax.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-tax-add */ public function apiTaxAdd($params) { return $this->_api->request('product.tax.add', $params); } /** * Perform method product.manufacturer.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-manufacturer-add */ public function apiManufacturerAdd($params) { return $this->_api->request('product.manufacturer.add', $params); } /** * Perform method product.currency.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/product-currency-add */ public function apiCurrencyAdd($params) { return $this->_api->request('product.currency.add', $params); } }
-
Order.php
<?php class Order extends Entity { /** * Perform method order.count.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-count */ public function apiCount($params) { return $this->_api->request('order.count', $params); } /** * Perform method order.list.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-list */ public function apiList($params) { return $this->_api->request('order.list', $params); } /** * Perform method order.info.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-info */ public function apiInfo($params) { return $this->_api->request('order.info', $params); } /** * Perform method order.find.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-find */ public function apiFind($params) { return $this->_api->request('order.find', $params); } /** * Perform method order.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-add */ public function apiAdd($params) { return $this->_api->request('order.add', $params); } /** * Perform method order.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-update */ public function apiUpdate($params) { return $this->_api->request('order.update', $params); } /** * Perform method order.status.list.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/order-status-list */ public function apiStatusList($params) { return $this->_api->request('order.status.list', $params); } }
-
Customer.php
<?php class Customer extends Entity { /** * Perform method customer.list.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-list */ public function apiList($params) { return $this->_api->request('customer.list', $params); } /** * Perform method customer.count.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-count */ public function apiCount($params) { return $this->_api->request('customer.count', $params); } /** * Perform method customer.info.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-info */ public function apiInfo($params) { return $this->_api->request('customer.info', $params); } /** * Perform method customer.find.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-find */ public function apiFind($params) { return $this->_api->request('customer.find', $params); } /** * Perform method customer.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-add */ public function apiAdd($params) { return $this->_api->request('customer.add', $params); } /** * Perform method customer.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-update */ public function apiUpdate($params) { return $this->_api->request('customer.update', $params); } /** * Perform method customer.delete.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/customer-delete */ public function apiDelete($params) { return $this->_api->request('customer.delete', $params); } }
-
Category.php
<?php class Category extends Entity { /** * Perform method category.list.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-list */ public function apiList($params) { return $this->_api->request('category.list', $params); } /** * Perform method category.count.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-count */ public function apiCount($params) { return $this->_api->request('category.count', $params); } /** * Perform method category.info.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-info */ public function apiInfo($params) { return $this->_api->request('category.info', $params); } /** * Perform method category.find.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-find */ public function apiFind($params) { return $this->_api->request('category.find', $params); } /** * Perform method category.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-add */ public function apiAdd($params) { return $this->_api->request('category.add', $params); } /** * Perform method category.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-update */ public function apiUpdate($params) { return $this->_api->request('category.update', $params); } /** * Perform method category.delete.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-delete */ public function apiDelete($params) { return $this->_api->request('category.delete', $params); } /** * Perform method category.assign.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-assign */ public function apiAssign($params) { return $this->_api->request('category.assign', $params); } /** * Perform method category.unassign.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-unassign */ public function apiUnassign($params) { return $this->_api->request('category.unassign', $params); } /** * Perform method category.image.add.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-image-add */ public function apiImageAdd($params) { return $this->_api->request('category.image.add', $params); } /** * Perform method category.image.update.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-image-update */ public function apiImageUpdate($params) { return $this->_api->request('category.image.update', $params); } /** * Perform method category.image.delete.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/category-image-delete */ public function apiImageDelete($params) { return $this->_api->request('category.image.delete', $params); } }
-
Cart.php
<?php class Cart extends Entity { /** * Perform method cart.create.json * * @param array $params List params for method * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.com/cart-create */ public function apiCreate($params) { return $this->_api->request('cart.create', $params); } /** * Perform method cart.validate.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.dev/cart-validate */ public function apiValidate() { return $this->_api->request('cart.validate'); } /** * Perform method cart.list.json * * @return stdClass Return cart list supported in API2Cart * * @throws Exception * * @see http://docs.api2cart.dev/cart-list */ public function apiList() { return $this->_api->request('cart.list'); } /** * Perform method cart.bridge.json * * @return stdClass Return bridge key and store key * * @throws Exception * * @see http://docs.api2cart.dev/cart-bridge */ public function apiBridge() { return $this->_api->request('cart.bridge'); } /** * Perform method cart.delete.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.dev/cart-delete */ public function apiDelete() { return $this->_api->request('cart.delete'); } /** * Perform method cart.disconnect.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.dev/cart-disconnect */ public function apiDisconnect() { return $this->_api->request('cart.disconnect'); } /** * Perform method cart.methods.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.dev/cart-methods */ public function apiMethods() { return $this->_api->request('cart.methods'); } /** * Perform method bridge.download.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.dev/bridge.download */ public function apiBridgeDownload() { return $this->_api->request('bridge.download'); } }
-
Bridge.php
<?php class Bridge extends Entity { /** * Perform method bridge.download.json * * @return stdClass * * @throws Exception * * @see http://docs.api2cart.dev/bridge-download */ public function apiDownload() { return $this->_api->request('bridge.download'); } }
-
Product.php
-
Api2Cart
-
TestData.php
<?php /** * This class contains parameters' tests for API2Cart's methods testing. */ class TestData { public static function data($method) { $methodName = $method['section'] . ucfirst($method['method']); return TestData::$methodName(); } /** * Test params for cart.create method * * @return array * * @see http://docs.api2cart.dev/cart-create */ public static function cartApiCreate() { return array( 'cart_id' => 'BigcommerceApi', 'store_url' => 'http://example.com', 'verify' => 'false', 'store_key' => 'ab37fc230bc5df63a5be1b11220949be', 'AdminAccount' => 'admin', 'ApiPath' => 'http://example.com/api/v1', 'ApiKey' => '6b89704cd75738cb0f9f6468d5462aba', ); } /** * Test params for cart.validate method * * @return array * * @see http://docs.api2cart.dev/cart-validate */ public static function cartApiValidate() { return array(); } /** * Test params for cart.list method * * @return array * * @see http://docs.api2cart.dev/cart-list */ public static function cartApiList() { return array(); } /** * Test params for cart.bridge method * * @return array * * @see http://docs.api2cart.dev/cart-bridge */ public static function cartApiBridge() { return array(); } /** * Test params for cart.delete method * * @return array * * @see http://docs.api2cart.dev/cart-delete */ public static function cartApiDelete() { return array(); } /** * Test params for cart.disconnect method * * @return array * * @see http://docs.api2cart.dev/cart-disconnect */ public static function cartApiDisconnect() { return array(); } /** * Test params for cart.methods method * * @return array * * @see http://docs.api2cart.dev/cart-methods */ public static function cartApiMethods() { return array(); } /** * Test params for bridge.download method * * @return array * * @see http://docs.api2cart.dev/bridge-download */ public static function bridgeApiDownload() { return array(); } /** * Test params for product.list method * * @return array * * @see http://docs.api2cart.dev/product-list */ public static function productApiList() { return array( 'start' => 0, 'count' => 50, 'params' => 'id,name,price', ); } /** * Test params for product.count method * * @return array * * @see http://docs.api2cart.dev/product-count */ public static function productApiCount() { return array(); } /** * Test params for product.info method * * @return array * * @see http://docs.api2cart.dev/product-info */ public static function productApiInfo() { return array( 'id' => 8, ); } /** * Test params for product.find method * * @return array * * @see http://docs.api2cart.dev/product-find */ public static function productApiFind() { return array( 'find_value' => 'Water', 'find_where' => 'name', 'find_params' => 'case_sensitive', ); } /** * Test params for product.fields method * * @return array * * @see http://docs.api2cart.dev/product-fields */ public static function productApiFields() { return array(); } /** * Test params for product.add method * * @return array * * @see http://docs.api2cart.dev/product-add */ public static function productApiAdd() { return array( 'name' => 'Bag', 'model' => 'bag_01', 'description' => 'This is new product', 'price' => 99.9, 'quantity' => 12, ); } /** * Test params for product.update method * * @return array * * @see http://docs.api2cart.dev/product-update */ public static function productApiUpdate() { return array( 'id' => 8, 'price' => 89, ); } /** * Test params for product.image.add method * * @return array * * @see http://docs.api2cart.dev/product-image-add */ public static function productApiImageAdd() { return array( 'product_id' => 8, 'image_name' => 'bag-gray.png', 'type' => 'base,thumbnail', 'url' => 'http://docs.api2cart.com/img/logo.png', ); } /** * Test params for product.image.update method * * @return array * * @see http://docs.api2cart.dev/product-image-update */ public static function productApiImageUpdate() { return array( 'product_id' => 8, 'image_name' => 'bag-gray.png', 'type' => 'additional', 'label' => 'logo', ); } /** * Test params for product.image.delete method * * @return array * * @see http://docs.api2cart.dev/product-image-delete */ public static function productApiImageDelete() { return array( 'product_id' => 8, 'image_name' => 'bag-gray.png', ); } /** * Test params for product.option.add method * * @return array * * @see http://docs.api2cart.dev/product-option-add */ public static function productApiOptionAdd() { return array( 'name' => 'Color', 'type' => 'option_type_select', 'required' => 'true', ); } /** * Test params for product.option.assign method * * @return array * * @see http://docs.api2cart.dev/product-option-assign */ public static function productApiOptionAssign() { return array( 'product_id' => 8, 'option_id' => 10, ); } /** * Test params for product.option.value.add method * * @return array * * @see http://docs.api2cart.dev/product-option-value-add */ public static function productApiOptionValueAdd() { return array( 'option_id' => 10, 'option_value' => 'Green', ); } /** * Test params for product.option.value.assign method * * @return array * * @see http://docs.api2cart.dev/product-option-value-assign */ public static function productApiOptionValueAssign() { return array( 'product_option_id' => 150, 'option_value_id' => 45, ); } /** * Test params for product.variant.add method * * @return array * * @see http://docs.api2cart.dev/product-variant-add */ public static function productApiVariantAdd() { return array( 'product_id' => 8, 'name' => 'Bag Green XXL', 'model' => 'bag_01_green_xxl', 'attributes' => array( 'Color' => 'Green', 'Size' => 'XXL' ), ); } /** * Test params for product.variant.list method * * @return array * * @see http://docs.api2cart.dev/product-variant-list */ public static function productApiVariantList() { return array( 'product_id' => 8, ); } /** * Test params for product.variant.count method * * @return array * * @see http://docs.api2cart.dev/product-variant-count */ public static function productApiVariantCount() { return array( 'product_id' => 8, ); } /** * Test params for product.variant.update method * * @return array * * @see http://docs.api2cart.dev/product-variant-update */ public static function productApiVariantUpdate() { return array( 'product_variant_id' => 18, 'quantity' => 5, ); } /** * Test params for product.variant.delete method * * @return array * * @see http://docs.api2cart.dev/product-variant-delete */ public static function productApiVariantDelete() { return array( 'product_variant_id' => 18, ); } /** * Test params for product.tax.add method * * @return array * * @see http://docs.api2cart.dev/product-tax-add */ public static function productApiTaxAdd() { return array( 'product_id' => 8, 'name' => 'ECO-Tax', 'tax_rates' => array( array( 'name' => 'VAT (17.2%)', 'type' => 'type_value_percent', 'value' => 17.2, ), array( 'name' => 'EKOS', 'type' => 'type_value_fixed', 'value' => 56, ) ), ); } /** * Test params for product.manufacturer.add method * * @return array * * @see http://docs.api2cart.dev/product-manufacturer-add */ public static function productApiManufacturerAdd() { return array( 'product_id' => 8, 'manufacturer' => 'Apple', ); } /** * Test params for product.manufacturer.add method * * @return array * * @see http://docs.api2cart.dev/product-currency-add */ public static function productApiCurrencyAdd() { return array( 'iso3' => 'USD', 'rate' => 1, ); } /** * Test params for category.list method * * @return array * * @see http://docs.api2cart.dev/category-list */ public static function categoryApiList() { return array( 'start' => 2, 'count' => 2, ); } /** * Test params for category.count method * * @return array * * @see http://docs.api2cart.dev/category-count */ public static function categoryApiCount() { return array(); } /** * Test params for category.info method * * @return array * * @see http://docs.api2cart.dev/category-info */ public static function categoryApiInfo() { return array( 'id' => 9 ); } /** * Test params for category.find method * * @return array * * @see http://docs.api2cart.dev/category-find */ public static function categoryApiFind() { return array( 'find_value' => 'Shoes' ); } /** * Test params for category.add method * * @return array * * @see http://docs.api2cart.dev/category-add */ public static function categoryApiAdd() { return array( 'name' => 'Shoes' ); } /** * Test params for category.update method * * @return array * * @see http://docs.api2cart.dev/category-update */ public static function categoryApiUpdate() { return array( 'id' => 9, 'avail' => 'false' ); } /** * Test params for category.delete method * * @return array * * @see http://docs.api2cart.dev/category-delete */ public static function categoryApiDelete() { return array( 'id' => 9, ); } /** * Test params for category.assign method * * @return array * * @see http://docs.api2cart.dev/category-assign */ public static function categoryApiAssign() { return array( 'product_id' => 8, 'category_id' => 9, ); } /** * Test params for category.unassign method * * @return array * * @see http://docs.api2cart.dev/category-unassign */ public static function categoryApiUnassign() { return array( 'product_id' => 8, 'category_id' => 9, ); } /** * Test params for category.image.add method * * @return array * * @see http://docs.api2cart.dev/category-image-add */ public static function categoryApiImageAdd() { return array( 'category_id' => 9, 'image_name' => 'cat.png', 'type' => 'base', 'url' => 'http://docs.api2cart.com/img/logo.png', ); } /** * Test params for category.image.update method * * @return array * * @see http://docs.api2cart.dev/category-image-update */ public static function categoryApiImageUpdate() { return array( 'category_id' => 9, 'image_name' => 'cat.png', 'label' => 'category Number 9', ); } /** * Test params for category.image.delete method * * @return array * * @see http://docs.api2cart.dev/category-image-delete */ public static function categoryApiImageDelete() { return array( 'category_id' => 9, 'image_name' => 'cat.png', ); } /** * Test params for order.count method * * @return array * * @see http://docs.api2cart.dev/order-count */ public static function orderApiCount() { return array(); } /** * Test params for order.list method * * @return array * * @see http://docs.api2cart.dev/order-list */ public static function orderApiList() { return array( 'start' => 0, 'count' => 10 ); } /** * Test params for order.info method * * @return array * * @see http://docs.api2cart.dev/order-info */ public static function orderApiInfo() { return array( 'order_id' => 28, ); } /** * Test params for order.find method * * @return array * * @see http://docs.api2cart.dev/order-find */ public static function orderApiFind() { return array( 'start' => 0, 'count' => 10 ); } /** * Test params for order.add method * * @return array * * @see http://docs.api2cart.dev/order-add */ public static function orderApiAdd() { return array( 'customer_email' => '[email protected]', 'order_status' => 'Complete', 'bill_first_name' => 'Adam', 'bill_last_name' => 'Smith', 'bill_address_1' => 'Green str. 35', 'bill_city' => 'Chicago', 'bill_postcode' => '12345', 'bill_state' => 'IL', 'bill_country' => 'US', 'total_price' => '23.56', 'order_item_id_1' => 8, 'order_item_name_1' => 'Bag', 'order_item_model_1' => 'bag_01', 'order_item_price_1' => 89, 'order_item_quantity_1' => 3, ); } /** * Test params for order.update method * * @return array * * @see http://docs.api2cart.dev/order-update */ public static function orderApiUpdate() { return array( 'order_id' => 28, 'order_status' => 'Pending' ); } /** * Test params for order.status.list method * * @return array * * @see http://docs.api2cart.dev/order-status-list */ public static function orderApiStatusList() { return array(); } /** * Test params for customer.list method * * @return array * * @see http://docs.api2cart.dev/customer-list */ public static function customerApiList() { return array(); } /** * Test params for customer.count method * * @return array * * @see http://docs.api2cart.dev/customer-count */ public static function customerApiCount() { return array(); } /** * Test params for customer.info method * * @return array * * @see http://docs.api2cart.dev/customer-info */ public static function customerApiInfo() { return array( 'id' => 7 ); } /** * Test params for customer.find method * * @return array * * @see http://docs.api2cart.dev/customer-find */ public static function customerApiFind() { return array( 'find_value' => '[email protected]', 'find_where' => 'first_name', 'find_params' => 'case_sensitive', ); } /** * Test params for customer.add method * * @return array * * @see http://docs.api2cart.dev/customer-add */ public static function customerApiAdd() { return array( 'email' => '[email protected]', 'first_name' => 'John', 'last_name' => 'Smith', ); } /** * Test params for customer.update method * * @return array * * @see http://docs.api2cart.dev/customer-update */ public static function customerApiUpdate() { return array( 'email' => '[email protected]', 'first_name' => 'Jack', 'last_name' => 'Smith', ); } /** * Test params for customer.delete method * * @return array * * @see http://docs.api2cart.dev/customer-delete */ public static function customerApiDelete() { return array( 'id' => 43, ); } }
-
Connector.php
<?php /** * Class Connector */ class Connector { /** * URL to API API2Cart * * @var string A2C_URL */ const A2C_URL = 'https://api.api2cart.com/v1.0/'; /** * @var array $_params */ private $_params = array(); /** * @param string $apiKey * @param string $storeKey */ public function __construct($apiKey, $storeKey = null) { $this->_params['api_key'] = $apiKey; if($storeKey !== null) { $this->_params['store_key'] = $storeKey; } } /** * Send request to API2Cart * * @param string $method Method's name * @param array $params Method's params list * * @return stdClass * * @throws Exception */ public function request($method, $params = array()) { $params = array_merge($this->_params,$params); $ch = curl_init(); $url = self::A2C_URL . $method . '.json?' . http_build_query($params); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 300); curl_setopt($ch, CURLINFO_HEADER_OUT,true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; " . "rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); curl_setopt($ch, CURLOPT_HTTPHEADER, array ( "Accept: */*", )); try { $response = trim(curl_exec($ch)); if(curl_errno($ch) != CURLE_OK) { throw new Exception(curl_error($ch)); } curl_close($ch); return $this->validate($response); } catch (Exception $e) { throw $e; } } /** * Received response validation * * @param string $response Method's response * * @return stdClass * * @throws Exception */ private function validate($response) { $result = json_decode($response); if($result === null) { throw new Exception('Response NULL'); } if($result->return_code != 0) { throw new Exception($result->return_message, $result->return_code); } return $result->result; } }
-
TestData.php
-
Entity.php
-
index.php
<?php /** * This file is used for methods testing. Run this file from console or from web browser * using the commands below: * * Command Line * php run.php %method%, * * %method% - define the method you need to run (for example, cart.create, product.list, etc.). * * Browser * http://localhost/code_sample_php/run.php?method=%method% * * %method% - define the method you need to run (for example, cart.create, product.list, etc.). * * Using test data of lib/Api2cart/TestData.php * * For more details visit http://docs.api2cart.com/ * */ include_once 'Api.php'; class Run { /** * @var Api $_api */ protected $_api; public function __construct() { $this->_api = new Api('PASTE_YOUR_API_KEY', 'PASTE_YOUR_STORE_KEY'); } /** * Validation input method name * * @return array * * @throws Exception */ protected function _validateMethodName() { if(isset($_SERVER['argc']) && isset($_SERVER['argc'])) { if($_SERVER['argc'] == 1) { throw new Exception('Please specify method name', 1); } $method = explode('.', $_SERVER['argv'][1]); } else { if(!isset($_GET['method'])) { throw new Exception('Please specify method name', 1); } $method = explode('.', $_GET['method']); } if(!isset($method[0]) || empty($method[0]) || !isset($method[1]) || empty($method[1])) { throw new Exception('Method ' . implode('.', $method) . ' not found', 1); } if(!method_exists($this->_api, $method[0])) { throw new Exception('Method ' . implode('.', $method) . ' not found', 1); } $section = $method[0]; unset($method[0]); $methodName = $this->_buildMethodName($method); $object = $this->_api->$section(); if(!method_exists($object, $methodName)) { throw new Exception('Method ' . $section .'.' . implode('.', $method) . ' not found', 1); } return array( 'section' => $section, 'method' => $methodName ); } /** * Generates function's name from method's name * * Example * product.option.value.add => apiOptionValueAdd * * @param array $method * * @return string */ protected function _buildMethodName($method) { $methodName = 'api'; foreach($method as $name) { $methodName .= ucfirst($name); } return $methodName; } /** * Run test method */ public function run() { try { $method = $this->_validateMethodName(); /** * Example * * $this->_api->product()->apiList() * $this->_api->order()->apiAdd() */ $result = call_user_func( array( call_user_func( array( $this->_api, $method['section'] ) ), $method['method'] ), TestData::data($method) ); //View result var_dump($result); } catch (Exception $e) { echo "#" . $e->getCode() . " " . $e->getMessage() . "\n"; } } } $run = new Run(); $run->run();
-
allViewMethod.php
<?php /** * This file contains all examples of how to use API2Cart's methods. * It's not recommended to run this file. * In order to test methods use index.php file. */ include_once 'lib/Api2Cart/Connector.php'; $api = new Connector('PASTE_YOUR_API_KEY','PASTE_YOUR_STORE_KEY'); /** * This section contains param's examples for cart's methods * * @see http://docs.api2cart.com/cart */ /** * cart.create * * @see http://docs.api2cart.com/cart-create */ $params = array( 'cart_id' => 'BigcommerceApi', 'store_url' => 'http://example.com', 'verify' => 'false',//set this param=false for test only 'store_key' => 'ab37fc230bc5df63a5be1b11220949be', 'AdminAccount' => 'admin', 'ApiPath' => 'http://example.com/api/v1', 'ApiKey' => '6b89704cd75738cb0f9f6468d5462aba', ); try { /** * Return stdClass */ $result = $api->request('cart.create', $params); echo 'Store Key: ' . $result->store_key; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * cart.validate * * @see http://docs.api2cart.com/cart-validate */ try { /** * Return stdClass */ $result = $api->request('cart.validate'); echo 'Status: ' . $result->status; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * cart.list * * @see http://docs.api2cart.com/cart-list */ try { /** * Return stdClass */ $result = $api->request('cart.list'); echo "Supported cart count: " . $result->supported_carts_count . "\n"; foreach($result->supported_carts[0]->cart as $cart) { echo "Cart ID: " . $cart->cart_id . "\n" . "Cart Name: " . $cart->cart_name . "\n" . "Cart Versions: " . $cart->cart_versions . "\n"; echo "Params: \n"; foreach($cart->params[0] as $name => $value) { echo " - " . $name . "\n"; } echo "\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * cart.bridge * * @see http://docs.api2cart.com/cart-bridge */ try { /** * Return stdClass */ $result = $api->request('cart.bridge'); echo "Store Key: " . $result->store_key . "\n" . "Bridge download URL: " . $result->bridge; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * cart.delete * * @see http://docs.api2cart.com/cart-delete */ try { /** * Return stdClass */ $result = $api->request('cart.delete'); echo 'Message: ' . $result->store;//Store http://www.mystore.com is removed! } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * cart.disconnect * * @see http://docs.api2cart.com/cart-disconnect */ try { /** * Return stdClass */ $result = $api->request('cart.disconnect'); echo 'Status: ' . $result->status; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * cart.methods * * @see http://docs.api2cart.com/cart-methods */ try { /** * Return stdClass */ $result = $api->request('cart.methods'); echo "List methods: \n"; foreach($result->method as $name) { echo $name . "\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * bridge.download * * @see http://docs.api2cart.com/bridge-methods */ try { /** * Return stdClass */ $result = $api->request('bridge.download'); } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * This section contains param's examples for product's methods * * @see http://docs.api2cart.com/product */ /** * product.list * * @see http://docs.api2cart.com/product-list */ $params = array( 'start' => 0, 'count' => 50, 'params' => 'id,name,price', ); try { /** * Return stdClass */ $result = $api->request('product.list', $params); echo "Products count: " . $result->products_count . "\n"; foreach($result->product as $product) { echo "Product ID: " . $product->id . "\n" . "Name: " . $product->name . "\n" . "Price: " . $product->price . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.count * * @see http://docs.api2cart.com/product-count */ $params = array( 'created_from' => '2014-07-29 13:45:52', ); try { /** * Return stdClass */ $result = $api->request('product.count', $params); echo "Products count: " . $result->products_count . "\n"; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.info * * @see http://docs.api2cart.com/product-info */ $params = array( 'id' => 8, 'params' => 'force_all' //all supported fields ); try { /** * Return stdClass */ $result = $api->request('product.info', $params); echo "ID: " . $result->id . "\n" . "Type: " . $result->type . "\n" . "Name: " . $result->name . "\n" . "Price: " . $result->price; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.find * * @see http://docs.api2cart.com/product-find */ $params = array( 'find_value' => 'egestas', 'find_where' => 'name', 'find_params' => 'case_sensitive', ); try { /** * Return stdClass */ $result = $api->request('product.find', $params); echo "Search result count: " . $result->products_count . "\n\n"; foreach($result->product[0] as $product) { echo "Product ID: " . $product->id . "\n" . "Name: " . $product->name . "\n" . "Description: " . $product->description . "\n" . "Price: " . $product->price . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.fields * * @see http://docs.api2cart.com/product-fields */ try { /** * Return stdClass */ $result = $api->request('product.fields'); echo "List fields: \n"; foreach($result->product_fields[0] as $name => $name) { echo $name . "\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.add * * @see http://docs.api2cart.com/product-add */ $params = array( 'name' => 'Bag', 'model' => 'bag_01', 'description' => 'This is new product', 'price' => 99.9, 'quantity' => 12, ); try { /** * Return stdClass */ $result = $api->request('product.add', $params); echo 'Product ID: ' . $result->product_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.update * * @see http://docs.api2cart.com/product-update */ $params = array( 'id' => 69, 'price' => 89, ); try { /** * Return stdClass */ $result = $api->request('product.update', $params); echo 'Updated items: ' . $result->updated_items; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.image.add * * @see http://docs.api2cart.com/product-image-add */ $params = array( 'product_id' => 69, 'image_name' => 'bag-gray.png', 'type' => 'base,thumbnail', 'url' => 'http://docs.api2cart.com/img/logo.png', ); try { /** * Return stdClass */ $result = $api->request('product.image.add', $params); echo 'Image path: ' . $result->image_path;//this value is used in image.update and image.delete methods } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.image.update * * @see http://docs.api2cart.com/product-image-update */ $params = array( 'product_id' => 69, 'image_name' => 'data/product/main/product_69_bag-gray.png', 'type' => 'base', 'label' => 'logo', ); try { /** * Return stdClass */ $result = $api->request('product.image.update', $params); //True/False echo 'Updated: ' . $result->updated; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.image.delete * * @see http://docs.api2cart.com/product-image-delete */ $params = array( 'product_id' => 69, 'image_name' => 'data/product/main/product_69_bag-gray.png', ); try { /** * Return stdClass */ $result = $api->request('product.image.delete', $params); //True/False echo 'Deleted: ' . $result->deleted; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.option.add * * @see http://docs.api2cart.com/product-option-add */ $params = array( 'name' => 'Color', 'type' => 'option_type_select', 'required' => 'true', ); try { /** * Return stdClass */ $result = $api->request('product.option.add', $params); echo "Option id: " . $result->option_id . "\n" . "Product option id: " . $result->product_option_id;//null if not specify product_id } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.option.assign * * @see http://docs.api2cart.com/product-option-assign */ $params = array( 'product_id' => 69, 'option_id' => 13, ); try { /** * Return stdClass */ $result = $api->request('product.option.assign', $params); echo "Product option id: " . $result->product_option_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.option.value.add * * @see http://docs.api2cart.com/product-option-value-add */ $params = array( 'option_id' => 13, 'option_value' => 'Green', ); try { /** * Return stdClass */ $result = $api->request('product.option.value.add', $params); echo "Option value id: " . $result->option_value_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.option.value.assign * * @see http://docs.api2cart.com/product-option-value-assign */ $params = array( 'product_option_id' => 330, 'option_value_id' => 49, ); try { /** * Return stdClass */ $result = $api->request('product.option.value.assign', $params); echo "Product option value id: " . $result->product_option_value_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.variant.add * * @see http://docs.api2cart.com/product-variant-add */ $params = array( 'product_id' => 8, 'name' => 'Bag Green XXL', 'model' => 'bag_01_green_xxl', 'attributes' => array( 'Color' => 'Green', 'Size' => 'XXL' ), ); try { /** * Return stdClass */ $result = $api->request('product.variant.add', $params); echo "Product variant id: " . $result->product_variant_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.variant.list * * @see http://docs.api2cart.com/product-variant-list */ $params = array( 'product_id' => 8, ); try { /** * Return stdClass */ $result = $api->request('product.variant.list', $params); echo "Variant list: \n"; foreach($result->variant[0] as $variant) { echo "Variant ID: " . $variant->id . "\n" . "Name: " . $variant->name . "\n" . "Description: " . $variant->description . "\n" . "Price: " . $variant->price . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.variant.count * * @see http://docs.api2cart.com/product-variant-count */ $params = array( 'product_id' => 8, ); try { /** * Return stdClass */ $result = $api->request('product.variant.count', $params); echo 'Variants count: ' . $result->variants_count; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.variant.update * * @see http://docs.api2cart.com/product-variant-update */ $params = array( 'product_variant_id' => 20, 'quantity' => 5, ); try { /** * Return stdClass */ $result = $api->request('product.variant.update', $params); echo 'Updated items: ' . $result->updated_items; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.variant.delete * * @see http://docs.api2cart.com/product-variant-delete */ $params = array( 'product_variant_id' => 20, ); try { /** * Return stdClass */ $result = $api->request('product.variant.delete', $params); echo 'Deleted: ' . $result->deleted; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.tax.add * * @see http://docs.api2cart.com/product-tax-add */ $params = array( 'product_id' => 69, 'name' => 'ECO-Tax', 'tax_rates' => array( array( 'name' => 'VAT (17.2%)', 'type' => 'type_value_percent', 'value' => 17.2, ), array( 'name' => 'EKOS', 'type' => 'type_value_fixed', 'value' => 56, ) ), ); try { /** * Return stdClass */ $result = $api->request('product.tax.add', $params); echo 'Tax class id: ' . $result->tax_class_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.manufacturer.add * * @see http://docs.api2cart.com/product-manufacturer-add */ $params = array( 'product_id' => 69, 'manufacturer' => 'Apple', ); try { /** * Return stdClass */ $result = $api->request('product.manufacturer.add', $params); echo 'Manufacturer id: ' . $result->manufacturer_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * product.currency.add * * @see http://docs.api2cart.com/product-currency-add */ $params = array( 'iso3' => 'USD', 'rate' => 1, ); try { /** * Return stdClass */ $result = $api->request('product.currency.add', $params); echo 'Currency id: ' . $result->currency_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * This section contains param's examples for category's methods * * @see http://docs.api2cart.com/category */ /** * category.list * * @see http://docs.api2cart.com/category-list */ $params = array( 'start' => 2, 'count' => 2, ); try { /** * Return stdClass */ $result = $api->request('category.list', $params); echo "Categories count: " . $result->categories_count . "\n" . "Categories list: \n"; foreach($result->category[0] as $category) { echo "Category ID: " . $category->id . "\n" . "Parent ID: " . $category->parent_id . "\n" . "Name: " . $category->name . "\n" . "Description: " . $category->description . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.count * * @see http://docs.api2cart.com/category-count */ try { /** * Return stdClass */ $result = $api->request('category.count'); echo 'Categories count: ' . $result->categories_count; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.info * * @see http://docs.api2cart.com/category-info */ $params = array( 'id' => 20 ); try { /** * Return stdClass */ $result = $api->request('category.info', $params); echo "Category ID: " . $result->id . "\n" . "Parent ID: " . $result->parent_id . "\n" . "Name: " . $result->name . "\n" . "description: " . $result->description . "\n"; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.find * * @see http://docs.api2cart.com/category-find */ $params = array( 'find_value' => 'Shoes', 'find_where' => 'name' ); try { /** * Return stdClass */ $result = $api->request('category.find', $params); echo "Search result count: " . $result->categories_count . "\n" . "Category list: \n"; foreach($result->category[0] as $category) { echo "Category ID: " . $category->id . "\n" . "Name: " . $category->name . "\n" . "Description: " . $category->description . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.add * * @see http://docs.api2cart.com/category-add */ $params = array( 'name' => 'Shoes' ); try { /** * Return stdClass */ $result = $api->request('category.add', $params); echo "Category id: " . $result->category_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.update * * @see http://docs.api2cart.com/category-update */ $params = array( 'id' => 20, 'avail' => 'false', 'meta_title' => 'meta title for category' ); try { /** * Return stdClass */ $result = $api->request('category.update', $params); echo 'Updated items: ' . $result->updated_items; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.delete * * @see http://docs.api2cart.com/category-delete */ $params = array( 'id' => 20, ); try { /** * Return stdClass */ $result = $api->request('category.delete', $params); echo 'Deleted: ' . $result->deleted; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.assign * * @see http://docs.api2cart.com/category-assign */ $params = array( 'product_id' => 69, 'category_id' => 20, ); try { /** * Return stdClass */ $result = $api->request('category.assign', $params); echo 'Assign'; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.unassign * * @see http://docs.api2cart.com/category-unassign */ $params = array( 'product_id' => 69, 'category_id' => 20, ); try { /** * Return stdClass */ $result = $api->request('category.unassign', $params); echo 'Unassign'; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.image.add * * @see http://docs.api2cart.com/category-image-add */ $params = array( 'category_id' => 20, 'image_name' => 'shoes.png', 'type' => 'base', 'url' => 'http://docs.api2cart.com/img/logo.png', ); try { /** * Return stdClass */ $result = $api->request('category.image.add', $params); //this value is used in image.update and image.delete methods echo 'Image path: ' . $result->image_path; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.image.update * * @see http://docs.api2cart.com/category-image-update */ $params = array( 'product_id' => 69, 'image_name' => 'data/category/main/category_20_shoes.png', 'type' => 'base', 'label' => 'logo', ); try { /** * Return stdClass */ $result = $api->request('category.image.update', $params); //True/False echo 'Updated: ' . $result->updated; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * category.image.delete * * @see http://docs.api2cart.com/category-image-delete */ $params = array( 'category_id' => 20, 'image_name' => 'data/category/main/category_20_shoes.png', ); try { /** * Return stdClass */ $result = $api->request('category.image.delete', $params); //True/False echo 'Deleted: ' . $result->deleted; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * This section contains param's examples for order's methods * * @see http://docs.api2cart.com/order */ /** * order.count * * @see http://docs.api2cart.com/order-count */ try { /** * Return stdClass */ $result = $api->request('order.count'); echo 'Orders count: ' . $result->orders_count; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * order.list * * @see http://docs.api2cart.com/order-list */ $params = array( 'start' => 0, 'count' => 5, 'params' => 'id,customer,status' ); try { /** * Return stdClass */ $result = $api->request('order.list', $params); echo "Orders count: " . $result->orders_count . "\n" . "Orders list: \n"; foreach($result->order[0] as $order) { $customer = $order->customer[0]; $status = $order->status[0]; echo "Order ID: " . $order->id . "\n" . "Customer email: " . $customer->email . "\n" . "Order status: " . $status->name . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * order.info * * @see http://docs.api2cart.com/order-info */ $params = array( 'order_id' => 1, 'params' => 'id,customer,status' ); try { /** * Return stdClass */ $result = $api->request('order.info', $params); $customer = $result->customer[0]; $status = $result->status[0]; echo "Order ID: " . $result->id . "\n" . "Customer email: " . $customer->email . "\n" . "Order status: " . $status->name . "\n\n"; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * order.find * * @see http://docs.api2cart.com/order-find */ $params = array( 'start' => 0, 'count' => 5, 'params' => 'id,customer,status' ); try { /** * Return stdClass */ $result = $api->request('order.find', $params); echo "Orders count: " . $result->orders_count . "\n" . "Orders list: \n"; foreach($result->order[0] as $order) { $customer = $order->customer[0]; $status = $order->status[0]; echo "Order ID: " . $order->id . "\n" . "Customer email: " . $customer->email . "\n" . "Order status: " . $status->name . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * order.add * * @see http://docs.api2cart.com/order-add */ $params = array( 'customer_email' => '[email protected]', 'order_status' => 'Complete', 'bill_first_name' => 'Adam', 'bill_last_name' => 'Smith', 'bill_address_1' => 'Green str. 35', 'bill_city' => 'Chicago', 'bill_postcode' => '12345', 'bill_state' => 'IL', 'bill_country' => 'US', 'total_price' => '23.56', 'order_item_id_1' => 8, 'order_item_name_1' => 'Bag', 'order_item_model_1' => 'bag_01', 'order_item_price_1' => 89, 'order_item_quantity_1' => 3, ); try { /** * Return stdClass */ $result = $api->request('order.add', $params); echo 'Order ID: ' . $result->order_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * order.update * * @see http://docs.api2cart.com/order-update */ $params = array( 'order_id' => 11, 'order_status' => 'Pending', 'comment' => 'Order comment' ); try { /** * Return stdClass */ $result = $api->request('order.update', $params); echo 'Updated items: ' . $result->updated_items; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * order.status.list * * @see http://docs.api2cart.com/order-status-list */ try { /** * Return stdClass */ $result = $api->request('order.status.list'); echo "Count statuses: " . $result->count . "\n" . "Order statuses list: \n"; foreach($result->cart_order_statuses[0]->status[0] as $status) { echo $status->name . "\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * This section contains param's examples for customer's methods * * @see http://docs.api2cart.com/order */ /** * customer.list * * @see http://docs.api2cart.com/customer-list */ $params = array( 'start' => 0, 'count' => 5 ); try { /** * Return stdClass */ $result = $api->request('customer.list', $params); echo "Customers list: \n"; foreach($result->customer[0] as $customer) { echo "Customer ID: " . $customer->id . "\n" . "Email: " . $customer->email . "\n" . "First Name: " . $customer->first_name . "\n" . "Last Name: " . $customer->last_name . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * customer.count * * @see http://docs.api2cart.com/customer-count */ try { /** * Return stdClass */ $result = $api->request('customer.count'); echo 'Customers count: ' . $result->customers_count; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * customer.info * * @see http://docs.api2cart.com/customer-info */ $params = array( 'id' => 1, 'params' => 'force_all' ); try { /** * Return stdClass */ $result = $api->request('customer.info', $params); echo "Customer ID: " . $result->id . "\n" . "Email: " . $result->email . "\n" . "First Name: " . $result->first_name . "\n" . "Last Name: " . $result->last_name; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * customer.find * * @see http://docs.api2cart.com/customer-find */ $params = array( 'find_value' => 'Emmanuel', 'find_where' => 'first_name', 'find_params' => 'case_sensitive', ); try { /** * Return stdClass */ $result = $api->request('customer.find', $params); echo "Search result count: " . $result->customers_count . "\n" . "Customers list: \n"; foreach($result->customer[0] as $customer) { echo "Customer ID: " . $customer->id . "\n" . "Email: " . $customer->email . "\n" . "First Name: " . $customer->first_name . "\n" . "Last Name: " . $customer->last_name . "\n\n"; } } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * customer.add * * @see http://docs.api2cart.com/customer-add */ $params = array( 'email' => '[email protected]', 'first_name' => 'John', 'last_name' => 'Smith', ); try { /** * Return stdClass */ $result = $api->request('customer.add', $params); echo 'Customer ID: ' . $result->customer_id; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * customer.update * * @see http://docs.api2cart.com/customer-update */ $params = array( 'id' => 11, 'first_name' => 'Jack', 'last_name' => 'Smith', ); try { /** * Return stdClass */ $result = $api->request('customer.update', $params); echo 'Updated items: ' . $result->updated_items; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); } /** * customer.delete * * @see http://docs.api2cart.com/customer-delete */ $params = array( 'id' => 11, ); try { /** * Return stdClass */ $result = $api->request('customer.delete', $params); echo 'Deleted: ' . $result->deleted; } catch (Exception $e) { echo '#' . $e->getCode() . ' ' . $e->getMessage(); }
-
README.txt
index.php --------------------------------------------------------------------------------------- This file is used for methods testing. Run this file from console or from web browser using the commands below: Command Line php run.php %method%, %method% - define the method you need to run (for example, cart.create, product.list, etc.). Browser http://localhost/code_sample_php/run.php?method=%method% %method% - define the method you need to run (for example, cart.create, product.list, etc.). For more details visit http://docs.api2cart.com/ allViewMethod --------------------------------------------------------------------------------------- This file contains all examples of how to use API2Cart's methods.
-
Api.php
<?php /** * This class initialize and include all major classes to use API2Cart. */ include_once 'lib/Api2Cart/TestData.php'; include_once 'lib/Entity.php'; include_once 'lib/Entities/Cart.php'; include_once 'lib/Entities/Bridge.php'; include_once 'lib/Entities/Product.php'; include_once 'lib/Entities/Category.php'; include_once 'lib/Entities/Order.php'; include_once 'lib/Entities/Customer.php'; class Api { /** * @var Cart $_cart */ protected $_cart; /** * @var Bridge $_bridge */ protected $_bridge; /** * @var Product $_product */ protected $_product; /** * @var Category $_category */ protected $_category; /** * @var Order $_order */ protected $_order; /** * @var Customer $_customer */ protected $_customer; public function __construct($apiKey, $storeKey) { $this->_cart = new Cart ($apiKey, $storeKey); $this->_bridge = new Bridge ($apiKey, $storeKey); $this->_product = new Product ($apiKey, $storeKey); $this->_category = new Category ($apiKey, $storeKey); $this->_order = new Order ($apiKey, $storeKey); $this->_customer = new Customer ($apiKey, $storeKey); } /** * @return Cart * * @throws Exception */ public function cart() { return $this->_cart; } /** * @return Bridge * * @throws Exception */ public function bridge() { return $this->_bridge; } /** * @return Product */ public function product() { return $this->_product; } /** * @return Category */ public function category() { return $this->_category; } /** * @return Order */ public function order() { return $this->_order; } /** * @return Customer */ public function customer() { return $this->_customer; } }
View code sample from GitHub