PHP Client

hublifyConnector class

A slim ready-to-use php-client for accessing the api in less than 3 minutes

hublifyConnector

Folgende beispielhafte Implementierung eines PHP-basierten Hublify-Clients.

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


    /**
     * @var string The url of your hublify-server, including the base path to api-resource.
     *      Example:  "https://xyz.hublify.io/api/"
     */
    protected  $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:  "eos_10"
     */
    protected  $api_version = 'eos_10';

    /**
     * @var string The username with which you want to connect to the hublify-server.
     *      Example: "foo.bar"
     */
    protected  $api_user = null;

    /**
     * @var string The dynamically acquired session-id-token provided from the hublify-server.
     *      We'll get this once we init the api-session and store it here for all further api-calls.
     *      As long as this hublifyConnector-instance lives. 
     */
    protected  $api_sessId = null;


    /**
     * hublifyConnector constructor.
     *
     * Automatically initializes an api session.
     *
     * @param $url string The url of your hublify-server, including the base path to api-resource.
     *        Example:  "https://xyz.hublify.io/api/"
     *
     * @param $username string The hublify-username with which you want to connect to the hublify-server.
     *        Example: "foo.bar"
     *
     * @param $userpwd string The password of that hublify-username.
     *        This ist not stored anywhere in this instance.
     */
    function __construct($url, $username, $userpwd) {
        $this->initSession($url, $username, $userpwd);
    }


    /**
     * Init the api-session
     *
     * This is usually called only once and directly in the beginning of this instance's lifecycle.
     *
     * We will connect and login to the api with username and password and in case of success we will
     * receive a api-session-id in return. This will be stored internally and needs to be used on all
     * further api-calls for authentication.
     *
     * @param $url string The url of your hublify-server, including the base path to api-resource.
     *        Example:  "https://xyz.hublify.io/api/"
     *
     * @param $username string The hublify-username with which you want to connect to the hublify-server.
     *        Example: "foo.bar"
     *
     * @param $userpwd string The password of that hublify-username.
     *        This ist not stored anywhere in this instance.
     *
     * @return bool Returns TRUE on login-success, else FALSE.
     */
    public function initSession($url, $username, $userpwd) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url.'login');
        $d = json_encode(array('username' => $username, 'userpwd' => $userpwd));
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $d);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $loginR = json_decode(curl_exec($ch),true);

        curl_close($ch);

        if(isset($loginR['data']['sessid'])) {

            $this->api_sessId = $loginR['data']['sessid'];

            $this->api_url = $url;
            $this->api_username = $url;

            return true;

        } else {

            $this->api_sessId = null;
            echo "ERROR connecting to api: ".$url;
        }
        return false;
    }


    /**
     * Calls an api-resource
     *
     * You need to have a valid API-Session instantiated...
     *
     * @param $function string The api-resource you want to call.
     *        Example:  "product_getlist".
     *
     * @param array $params array Optional parameters for the called function.
     *        Which parameters are possible or required depends on the function you call.
     *
     * @return array|null
     */
    public function call($function, $params = null) {

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

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->api_url . $this->api_version . '/' . $function);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array( "API-SessID: " . $this->api_sessId ));
            curl_setopt($ch, CURLOPT_POST, true);
            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);

            $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 -sessId set.";
        }
        return null;
    }

}

Beispiel

Folgendes Beispiel benutzt die hublifyConnector Class, um eine Produktliste abzurufen.

index.php

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

    require_once ('hublifyconnector.class.php');

    // Provide your Hublify-server and -credentials
    $hublifySrvUrl = 'https://{your-hublify-url}/api/';
    $hublifyUserName = '<your username>';
    $hublifyPwd = '<your pwd>';

    // Create hublify-api-connector (and init connection):
    $hublify = new hublifyConnector($hublifySrvUrl, $hublifyUserName, $hublifyPwd);

    // Call api:  "product list"
    $r = $hublify->call(
        'product_getlist',
        [
            "fields"=> [ "name","pcode" ],
            'sql' => [ 'limit' => [ 'max' => 3]]]
    );

    print_r($r);

Ausgabe

Array
(
    [0] => Array
        (
            [pcode] => GX00009
            [name] => T-Shirt Skywalker
        )

    [1] => Array
        (
            [pcode] => SW00123
            [name] => Laserschwert "Vader"
        )

    [2] => Array
        (
            [pcode] => SW00124
            [name] => Yoda Cape XXS
        )

)