Coder Social home page Coder Social logo

liteview-api's Introduction

Liteview API

StyleCI Build Status Latest Stable Version Total Downloads Latest Unstable Version

Liteview API is a PHP wrapper around the Liteview REST API. It provides a lightweight and flexible interface for communicating with Liteview's ecommerce fulfillment services.

Installation

Install via composer:

composer require gigasavvy/liteview-api

Basic Usage

To make a connection to the API, create a new instance of Liteview\Connection, passing in your username and appkey credentials to the constructor:

use Liteview\Connection;

$connection = new Connection('username', 'key');

Behind the scenes, an HTTP client is created with the proper headers and data needed to successfully access the API. To make calls to the API, simply call the supported HTTP verbs as methods of the Connection class. The first parameter is the resource URI to access. The second (optional) parameter is the body to send with the request. This should be a valid XML string.

// Get all supported order methods.
$orderResponse = $connection->get('order/methods');

// Submit a new order
$connection->post('order/submit', $xmlString);

XML Sucks

XML in a REST API is like hamburgers at a Mexican restaurant. Unfortunately, Liteview uses XML for the request and response payload. Fortunately, this package utilizes the Gestalt Configuration Package, allowing you to define default XML configurations and modify them as objects before sending a request.

To get started, head over to the Liteview API Documentation for examples of the XML payload for each resource. You can copy these exmaples into individual XML files and edit them as you would like. Once you have defined the default skeleton of your XML request(s), it is likely you will want to dynamically populate/modify different nodes of the XML document. This is where the Liteview\Api\Resources\Resource class comes in.

The Resource class extends the Gestalt\Configuration object and is intended to represent your XML payloads as a Gestalt Configuration object. It defines an additional toXml() method which you should call before passing it to an API request. Let's look at an example of using the Resource class to prepare a resource request:

use Liteview\Connection;
use Liteview\Api\Resources\Resource;
use Liteview\Api\Resources\ResourceLoader;

// Create a new Resource instance using the ResourceLoader, which
// converts the given XML file to an associative array.
$order = Resource::create(new ResourceLoader('order_submit.xml'));

$order->set('order_status', 'Active');

$connection = new Connection('username', 'key');
$connection->post('order/submit', $order->toXml());

Here we can see a full example of creating a Resource object from an XML file using the power of Gestalt's Custom Loaders. We can then use any of the Gestalt Configuration methods to modify our XML configuration before finally sending it off to the connection's post method.

With this flexibility we can define our XML once and modify it at runtime without worrying about XML syntax or the headache of working directly with a SimpleXMLElement or the like. Have a look at Gestalt's documentation for more information on using the package.

Appendix

Helpers

The Liteview API package defines some global helper functions that may be used as well.


string array_to_xml(array $data, SimpleXMLElement &$xml)

array_to_xml() takes an associative array of XML data and parses it into the given SimpleXMLElement.


array xml_to_array(mixed $xml)

xml_to_array() takes a UTF-8 encoded XML string or object and converts it into an associative array.


bool is_assoc(array $arr)

is_assoc() takes an array and determines whether it is associative or not.


Conclusion

More features and documentation coming soon.

liteview-api's People

Watchers

James Cloos avatar Kris Chase avatar Sam Rapaport avatar Corey Mangold avatar

Forkers

genflow

liteview-api's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.