Tutorial 1 - Understanding the API


This basic tutorial shows you how to protect an offer on your website. You will learn the basics of the Cleeng API. All in just 5 minutes. This tutorial is for websites that can execute PHP.

On the right, you can see little example, how content protection works. What is great about our solution, is that you can hide only a part of your content. E.g. Your article is about creating robots, your are describing how they work, what are the parameters, but only thing you are hiding is tutorial how to make one on your own.

After this tutorial you can sell anything digital: a few lines of code, a video embed (with domain restrictions), access to a form, how to build robots tutorial or ... be creative!

  1. What is Cleeng PHP SDK for ?

  2. Get prepared & protect your content.

  3. Testing payment



1. What is Cleeng PHP SDK ?

Cleeng SDK for PHP developers provides a rich set of server-side functionality for accessing Cleeng server-side API calls. Using it you can easily integrate your website with out platform. For more information go to PHP SDK section.

2. Protect your content.

Download the example files from Github, this includes the Cleeng PHP SDK. Place them on your server.

Very important!

In this tutorial, we gonna use default offer. How to create offers on your own you can read in Tutorial 2

2.1 How it all works

  • Integrate your website with Cleeng PHP SDK
  • Choose the part of content you want to protect
  • Create offer, describe your content (next tutorial)
  • Check if customer has access to particular offer
  • If yes, show it to him
  • If not, show Cleeng payment frame and earn money

2.2. Finally, let's start with some coding

Click to see a working demo: Example 1 - Protect your content.

and

Open file purchase.php from github

purchase.php :

<?php

$offerId = 'R688881907_NL'; // Default offer Id - for tests

// include PHP SDK
include_once('../cleeng-php-sdk/cleeng_api.php');
$cleengApi = new Cleeng_Api();
?>
<script type="text/javascript" src="<?php echo $cleengApi->getJsApiUrl() ?>"></script>
<script type="text/javascript">
CleengApi.trackOfferImpression("<?php echo $offerId ?>");

function cleengPurchase() {
    CleengApi.purchase("<?php echo $offerId ?>", function(result) {
        if (result.accessGranted) {
            window.location.reload();
        }
    });
}
</script>

... place the following within your HTML.
if ($cleengApi->isAccessGranted($offerId)) {
    echo 'This content is accessible when you purchase it! Place here the stuff you want to sell.';
} else {
    echo 'Content not accessible. <a href="javascript:cleengPurchase()">Buy</a>';
}
.....remainder of your webpage.

2.2.1. Basics

  • Include API

    include_once('../cleeng-php-sdk/cleeng_api.php') - make sure that path is correct

  • Create API object

    $cleengApi = new Cleeng_Api() - from now on you can use every method in API using $cleengApi variable

  • Offer Id

    $offerId = 'R688881907_NL' - this is Cleeng default offer Id, which you can use to test the code. In Tutorial 2 - Creating Offers, you can read how to create an offer and get Id from it.

2.2.2. Authorization

Defining the part of your content that you want to protect is the core of using Cleeng PHP SDK. Use $cleengApi->isAccessGranted() to validate if the visitor is authorized and if the offer should be revealed. You can place any piece of HTML between the tags.

if ($cleengApi->isAccessGranted($offerId)) {
    echo 'This content is accessible when you purchase it! Place here the stuff you want to sell.';
} else {
    echo 'Content not accessible. <a href="javascript:cleengPurchase()">Buy</a>';
}

If the user is not logged in or hasn't purchased our content, <a href="javascript:cleengPurchase()">Buy</a> you gonna display Buy button, which starts cleengPurchase() method.

2.2.3. Java Script

In the <head>...</head> of your HTML page put this code:

<script type="text/javascript" src="<?php echo $cleengApi->getJsApiUrl() ?>"></script>
<script type="text/javascript">
    CleengApi.trackOfferImpression("<?php echo $offerId ?>");
    function cleengPurchase() {
        CleengApi.purchase("<?php echo $offerId ?>", function (result) {
            window.location.reload();
        });
    }
</script>

As you can see, we've just included api.js file. From now on we can use:

  • CleengApi.trackOfferImpression() which is necessary to make statistics, count impression.

  • CleengApi.purchase() using that method, we create cleengPurchase() method

    which will be started after clicking on Buy <a href="javascript:cleengPurchase()">Buy</a>

Hurray!

You are now almost ready to protect and sell digital content from your own website!

The last important thing you should know is how to create offers,

Go to Creating Offers tutorial »

3. Testing payment

During testing, as you probably can see, you can't complete you purchase unless you pay with real money. In Tutorial How to use Cleeng sandbox, you can read how to use Sandbox to test payment and be able to finish the purchase process.