PHP examples

In this article:


    

    The PHP examples in this section uses the PHP SOAP module that can be found here: http://php.net/manual/en/book.soap.php.

    Select a PHP example below.

     

    Create a SOAP Client

    To set up a connection to a solution, a SoapClient object is first created. As an argument, the path to the WDSL-file (Web Service Description Language) is given.

    
            $Client = new SoapClient('https://api.hostedshop.io/service.wsdl');
        
    

    Please note that all examples in this section are via PHP soap client. When using this client to connect to the API, all arguments are required to be wrapped in an array, which is also the case in this section.

    The convention for using the functions in the PHP soap client is generally to transform arguments from the documentation to an array with argument-names as keys and argument value as the value. For example:

    Category_UpdateTitle (int $CategoryId, string $Title, string $LanguageISO)

    in the documentation. Here, the call via PHP soap client would be:

    Category_UpdateTitle (['CategoryId' => int, 'Title' => string, 'LanguageISO' => string])

     

    Connecting to a solution

    You can now connect to a specific solution: 

    
    $Client->Solution_Connect (array('Username' => 'some_username', 'Password' =>'some_password'));
    
    

     

    Select language

    The shopsystem and CMS support multiple languages. An extract from the API is always set in a specific language. This is set by specifying the desired language ISO code for the extract. The following example sets the language to Danish:

    
    $Client->Solution_SetLanguage(array('LanguageISO' => 'DK'));
    
    

    This affects both extracts, updates and the creation of new entities. The list of available languages and their ISO code for a specific solution can be viewed in the administration in Settings -> Languages and domains.

    Setting an ISO that is not in use will return an error code. Read more about error codes in section 4.

     

    Set dataformats

    To optimize the speed when retrieving larger amounts of data via the API, it is important to specify which data you want to extract. For example, if you want data for updating the stock status of products and variants, this could be specified as follows:

    
    $Client->Product_SetFields(array('Fields' => 'Id,Stock,Variants'));
    
    $Client->Product_SetVariantFields(array('Fields' => 'Id,Stock'));
    
    

    This ensures that the returned data objects only contain the specified fields, which reduces the size of the extract and the time it takes to transfer to the local application.

    For larger datasets, the practice to specify an extract as small as possible can make the extract routines and their run time more efficient. It is possible to specify datafields for a a variety of shop entities via:

    • Product_SetFields
    • Product_SetVariantFields
    • Order_SetFields
    • Order_SetOrderLineFields
    • User_Setfields

     

    Fetch products

    This part follows up on the scenario in 2.4 where you want to extract products and variants' stock status. If you want an extract of all products for a solution, it can be done as follows:

    
    $Client->Product_SetFields(array('Fields' => 'Id,Stock,Variants'));
    $Client->Product_SetVariantFields(array('Fields' => 'Id,Stock'));
    $ProductReturn = $Client->Product_GetAll();
    $Products = $ProductReturn->Product_GetAllResult->item;
    
    

    Note that the result is wrapped in the variable 'Product_getAllResult'. This is because of the SOAP document / literal wrapped style. A function 'x' will always return its result in an object variable of the name 'xResult'. If the result is another array of objects, this will be in the 'item' variable under 'xResult'

    An example of a simple application to print the retrieved data:

    
    foreach ($Products as $Product) {
    echo "Product: $Product->Id, stock: $Product->Stock\n";
    foreach ($Product->Variants->item as $Variant) {
    echo "Product: $Product->Id, variant: $Variant->Id stock: $Variant->Stock \n";
    }
    }
    
    

    There are also many options to specify which products you want in the extract that can be useful in different situations. For example, it is possible to retrieve products that have been updated within a given period. If you want eg. products that have been updated within the last week:

    :

    
    $StartDate = date('Y-m-d', time() – 60 * 60 * 24 * 7); 
    $Products = $Client->Product_GetByUpdatedDate(array('StartDate' => $StartDate));
    
    

     

    Fetch orders

    As with products, there is also a range of different functions for retrieving order data. To retrieve all data for a shop solution:

    
    $Client->Order_SetFields(array('Fields' => 'Id,DateDelivered,OrderComment'));
    $Client->Order_SetOrderLineFields(array('Fields' => 'Id,StockStatus'));
    $Orders = $Client->Order_GetAll();
    
    

    You can also retrieve orders created from and to a specific date that match one or more order statuses:

    
    $Orders = $Client->Order_GetByDate(array('Start' => '2009-01-01', 'End' => '2009-02-01', 'Status' => '3,6'));
    
    

    Retrieving all orders for periods 1/1 2009 to 1/2 2009 with order status 3 (Order Sent) or 6 (Completed). For the full documentation, see https://api.hostedshop.io/doc/.

     

    Create new product

    To create a product, first create an object or array with the relevant product data. Then, call the product creation method with data as an argument: 

    
    class ProductCreate {
               public $Title;
               public $LanguageIso;
               public $DescriptionLong;
    ...
    }
    
    $parameter                       = new ProductCreate();
    $parameter->Title                = 'A title';
    $parameter->DescriptionLong      = 'A long description';
    $parameter->LanguageIso          = 'DK';
    ...
    
    $ProductReturn = $Client->Product_Create(array('ProductData' => $parameter));
    $NewProductId = $ProductReturn->ProductCreateResult;
    
    

    The method returns the ID of the created product. Extended product data can then be created for the product, such as images and variants:

    
    class ProductVariantCreate {
               public $ProductId;
               public $ItemNumber;
               public $Price;
    ...
    }
    
    $parameter                       = new ProductVariantCreate();
    $parameter->ProductId            = $NewProductId;
    $parameter->ItemNumber           = 'variant43';
    $parameter->Price                = 40.2;
    ...
    
    $Client->Product_CreateVariant(array('VariantData' => $parameter));
    
    

    Note that the variant's ProductId is set to the ID of the newly created product to bind it to the product.

     

    Update product

    The procedure for updating a product is very similar to the method for creating new products. First, a data object or data array is defined and then a method is called with the object as an argument.

    
    class ProductUpdate {
               public $Id;
               public $Stock;
               public $CallForPrice; 
    }
    
    $parameter                       = new ProductUpdate();
    $parameter->Id                   = 1337;
    $parameter->Stock                = 5;
    $parameter->CallForPrice         = false;
    
    $Client->Product_Update(array ('ProductData' => $parameter));
    
    

    However, there are a number of differences. Firstly, you only need to set the fields in the object that you want to update. Thus, the above example updates Stock and CallForPrice for the product with id 1337, and leaves the remaining fields unchanged.

    In addition to the ID, it is possible to use the item number to identify products you want to update. Please note that this may update more than one product, as the item number is not necessarily unique:

    
    class ProductUpdate {
               public $ItemNumber;
               public $Title;
               public $LanguageISO;
    }
    
    $parameter                       = new ProductUpdate();
    $parameter->ItemNumber           = 'item234';
    $parameter->Title                = 'A product title';
    $parameter->LanguageISO          = 'DK';
    
    $Client->Product_Update(array ('ProductData' => $parameter));
    
    

    updates the Danish title of all products with item number 'item234' . The method returns an array of IDs for the updated products.

    If an ID is entered when updating a product, it will always be used as the identifying part. If you want to update the item number for a product, you can do so by specifying both the ID and the new item number:

    
    class ProductUpdate {
               public $Id;
               public $ItemNumber;
    }
    
    $parameter                       = new ProductUpdate();
    $parameter->Id                   = 1337;
    $parameter->ItemNumber           = 'nytvarenummer';
    
    $Client->Product_Update(array ('ProductData' => $parameter));
    
    

     

    Delete product

    To delete a product, specify which product to delete via its unique ID:

    
    $Client->Product_Delete(array ('ProductId' => 1337));
    
    

     

    Choose character set

    It is possible to set the character set for all text fields in input and output functions via Solution_SetEncoding. All calls made after Solution_SetEncoding will use the specified character set. This can be useful if you want to process data in a given character set. If ISO-8859-1 is desired, call

    
    $Client->Solution_SetEncoding(array ('Encoding' => 'ISO-8859-1'));
    
    

    If, on the other hand, you want html special characters instead of e.g. æ, ø and å, the following is called:

    
    $Client->Solution_SetEncoding(array ('Encoding' => 'HTML-ENTITIES'));
    
    

    Finally, UTF-8 can be used by calling

    
    $Client->Solution_SetEncoding(array ('Encoding' => 'UTF-8'));
    
    

    It is recommended to stick to one character set throughout all calls to avoid misspelling.