hublifyChannelConnector

Here you find an example for a PHP-based Hublify-Client for Channels.

/**
 * Class hublifyChannelConnector  ( DEMO CLASS ! )
 *
 * An easy-to-use class to call the hublify api to read or write data for your channel.
 *
 * This simple class uses only CURL-functions. It has no other dependencies.
 *
 */
class hublifyChannelConnector {

    /**
     * @var ?string The url of the hublify-server, including the base path to api-resource.
     *      Example:  "https://<your-account>.hublify.io/api/"
     */
    protected ?string $api_url = null;

    /**
     * @var string The required api-version addressed on the hublify-server.
     *      Leave this untouched. You actually will not have to change this, at all.
     *      Default:  "channel_10"
     */
    protected string $api_version = 'channel_10';

    /**
     * @var ?string The token with which you want to connect to the hublify-server.
     */
    protected ?string $api_token = null;


    /**
     * hublifyChannelConnector constructor.
     *
     * @param $url string The url of the hublify-server, including the base path to api-resource.
     *        Example:  "https://<your-account>.hublify.io/api/"
     *
     * @param $token string The hublify-token, with which you want to connect to the hublify-server
     */
    function __construct(string $url, string $token) {
        $this->init($url, $token);
    }


    /**
     * Init
     *
     * This is usually called only once and directly in the beginning of this instance's lifecycle.
     *
     * @param string $url  The url of the hublify-server, including the base path to api-resource.
     *        Example:  "https://<your-account>.hublify.io/api/"
     *
     * @param string $token The hublify-token with which you want to connect to the hublify-server.
     *
     * @return bool Returns TRUE on login-sucess, else FALSE.
     */
    public function init(string $url, string $token): bool
    {
         $this->api_token = $token;
         $this->api_url = $url;

         return true;
    }

    /**
     * Calls an api-resource
     *
     * @param string $function  The api-resource you want to call.
     *        Example:  "order_get".
     *
     * @param ?array $params array Optional parameters for the called function.
     *        Which parameters are possible or required depends on the function you call.
     * 
     * @param $stream bool Optional. Set true, if you expect a stream instead of json as a result           
     *
     * @return array|null
     */
    public function call(string $function, ?array $params = null, $stream = false)
    {

        if(strlen($this->api_url) > 0 && strlen($this->api_token) > 0) {

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->api_url . $this->api_version . '/' . $function);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array( "API-Channel-Token: " . $this->api_token ));
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            if (!is_null($params)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
            }

            $response = curl_exec($ch);
            curl_close($ch);

            if ($stream == true) {
                $r = $response;
            } else {
                $r = json_decode($response, true);
            }

            if (!is_null($r)) {
                return ($r);

            } else {
                echo "CALL error.";
                print_r($response);
            }
        } else {
            echo "CALL error. No API-url and/or -token set.";
        }
        return null;
    }
}

Examples

Pushing data

This example uses the hublifyConnector Class, to create an order in hublify.

index.php

/**
 *     C a l l i n g    t h e    H u b l i f y    A P I
 *
 *     Using the "hublifyChannelConnector"-class
 */

    require_once ('hublifyChannelConnector.class.php');

    // Provide your Hublify-server and -credentials
    $hublifySrvUrl = 'https://{your-hublify-url}/api/';
    $hublifyToken = '<your token>';

    // Create hublify-api-connector (and init connection):
    $hublify = new hublifyChannelConnector($hublifySrvUrl, $hublifyToken);

    // Call api:  "order create"
    $r = $hublify->call(
        'order_create',
           [
        'fields' =>  [
            'person' => [
                'p_personid_external' => 'ABC000012',
                'bday' => '1973-01-22',
                'telephone' => '+4940123456',
                'telephone_mobile' => '+491601234567',
                'email' => 'max@muster.de',
            ],
            'order' => [
                'o_orderid_external' => 'CH00012345',
                'invoice_company' => '',
                'invoice_gender' => 'male',
                'invoice_firstname' => 'Max',
                'invoice_lastname' => 'Muster',
                'invoice_addrextra' => '',
                'invoice_street' => 'Sample Street',
                'invoice_housenr' => '1b',
                'invoice_zip' => '22222',
                'invoice_town' => 'Hamburg',
                'invoice_country' => 'DE',
                'deliver_company' => NULL,
                'deliver_gender' => 'female',
                'deliver_firstname' => 'Maxi',
                'deliver_lastname' => 'Muster',
                'deliver_addrextra' => NULL,
                'deliver_street' => 'Second Street',
                'deliver_housenr' => '2a',
                'deliver_zip' => '11111',
                'deliver_town' => 'Berlin',
                'deliver_country' => 'DE',
                'orderitems' => [
                    [
                        'oi_pcode' => 'EK00001',
                        'oi_quantity' => 1
                    ],
                    [
                        'oi_pcode' => 'PC0001',
                        'oi_quantity' => 2
                    ]
                ]
            ]
        ]
    ]);

    print_r($r);

Printed result

Array
(
    [status] => 1
    [data] => Array
        (
            [personid] => KCHN-00012345
            [orderid] => ACHN-00123456
            [o_orderid_external] => CH00012345
            [order_state_code] => received
        )

)

Pulling data

This example uses the hublifyConnector Class to get details like the current status for a previously injected order.

index.php

/**
 *     C a l l i n g    t h e    H u b l i f y    A P I
 *
 *     Using the "hublifyChannelConnector"-class
 */

    require_once ('hublifyChannelConnector.class.php');

    // Provide your Hublify-server and -credentials
    $hublifySrvUrl = 'https://{your-hublify-url}/api/';
    $hublifyToken = '<your token>';

    // Create hublify-api-connector (and init connection):
    $hublify = new hublifyChannelConnector($hublifySrvUrl, $hublifyToken);

    // Call api:  "order get"
    $r = $hublify->call(
        'order_get',
        [
            "filter"=> ["orderid" => "ACHN-00123456"], // the Hublify internal orderid
        ]
    );

    print_r($r);

Ausgabe

Array
(
    [status] => 1
    [data] => Array
        (
            [personid] => KCHN-00012345
            [orderid] => ACHN-00123456
            [o_orderid_external] => YOUR-ID-12345
            [order_state_code] => received
        )

)