MyCloud Logo

MyCloud API Documentation

PHP SDK Overview

The MyCloud API PHP SDK provides a complete PHP implementation of the client side the API. With the PHP SDK, you do not need to worry about any of the low level details of programming with the API. Connectivity, authentication, model objects, logging, and other supporting code is all provided for you.

When working with the PHP SDK, you will work primarily with the API model objects. Model objects provide everything you need to work with any data related to your shop, as well as implement all of the calls to the API server needed to create, get and update that data.

Model Objects

All model objects in the PHP SDK are standard PHP classes and class objects. You can work with them the same as you would with any PHP class and object.

All model objects are contained in the PHP namespace MyCloud\Api\Model. For convenience and to simplify your code, you can add PHP use statements at the top of your code as in:

 use MyCloud\Api\Model\Customer;
 use MyCloud\Api\Model\Order;
 use MyCloud\Api\Model\Product;
This will allow you to refer to the object classes directly without specifying the namespace, as in:
 $order = new Order();
 $customer_list = Customer::all();

Model Attributes

The model objects provide attributes. These attributes use the PHP magic getter/setter paradigm. This means that in general you can work with the attributes as object fields. Furthermore, for each attribute the class will provide a getter and setter method call.

For example, the Customer model has a name attribute. Thus, you can access the value of the customer name with either of these programming constructs:

 $name = $customer->name;
 $name = $customer->getName();
 $customer->name = $name;
 $customer->setName( $name );

API Requests

API requests typically return model objects. You can then work with the objects the same as any PHP object. However, there are two cases where a model is not returned: errors and exceptions.

If an error occurs in an API request that does not cause the API to fail processing the request, you will get back an MCError object instead of a model object. Thus, for every API call that you make that expects an object to be returned, you must first check to see if that object is an instance of class MCError. If it is, the object will contain information regarding the error that occurred.

An example of handling both of these cases would look similar to this:

try {
	$product = Product::get( $product_id );

	if ( $product instanceof MCError ) {
		print "ERROR retrieving Product:" . PHP_EOL;
		print "      " . $product->getMessage() . PHP_EOL;
	} else {
		print_product_data( "Product: ", $product );
	}
} catch ( Exception $ex ) {
	print "EXCEPTION: " . $ex->getMessage() . PHP_EOL;
}

You can see that we first check to see if the returned object is an instanceof MCError. If it is not, then the object returned is one of the API models ready for you to use. We also wrap every SDK call inside of a try { ... } catch ( Exception $ex ) { ... } to make sure we catch any exceptions that may be thrown by the SDK code. Exceptions will contain further information about the error, which you can obtain with the getMessage() function.