Software API Integration code example

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this page

Any software can use API functionality. We can provide full code in a form of WordPress plugin which can be used as staring base for further implementation.

This is a sample usage on a licence key activation call:

The first part of the code consist of few constants definition which are being used later within the code.


//the url where the WooCommerce Software License plugin is being installed
define('SL_APP_API_URL',      'http://YourDomainWhereSoftwareManagement.com/index.php');

//the Software Unique ID as defined within product admin page
define('SL_PRODUCT_ID',           'APTO');


$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
define('SL_INSTANCE',             str_replace($protocol, "", get_bloginfo('wpurl')));

A simple activation calls will looks like this:

    $args = array(
                    'woo_sl_action'         => 'activate',
                        'licence_key'       => $license_key,
                        'product_unique_id'        => SL_PRODUCT_ID,
                        'domain'          => SL_INSTANCE
                    );
    $request_uri    = SL_APP_API_URL . '?' . http_build_query( $args );
    $data           = wp_remote_get( $request_uri );

    if(is_wp_error( $data ) || $data['response']['code'] != 200)
        {
            //there was a problem establishing a connection to the API server
        }

    $response_block = json_decode( wp_remote_retrieve_body( $data ) );
                      
    if(!is_array($response_block) || count($response_block) < 1)
        return $checked_data;

    //retrieve the last message within the $response_block
    $response_block = $response_block[count($response_block) - 1];
    if(isset($response_block->status))
        {
            if( $response_block->status == 'success' && ( $response_block->status_code == 's100' || $response_block->status_code == 's101' ) )
                {
                    //the license is active and the software is active
                    //doing further actions like saving the license and allow the plugin to run

                }
                else
                {
                    //there was a problem activating the license
                }
        }
        else
        {
            //there was a problem establishing a connection to the API server
        }

The API $data_body response include also a ‘message’ argument with detailed text information of the response. This is always an English text, which can be translated to the local site language using _e, __ and other WordPress translation functions.


By woocommerce-sl, posted on August 1, 2017

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments