PHP
Introduction
The PHP SDK is the most efficient way to secure the access to your articles through the SmartWall.
SDK v1 will be deprecated in the coming months, we suggest that you update your integration to SDK v2. More in Upgrading to v2.
Add the SDK in your PHP code
The PHP SDK is available through composer for download on packagist
Once downloaded add the SmartWall PHP SDK Library into your code. You can see a way of doing this in the example below.
<?php
require 'smartwall.php';
?>
Set up your SmartWall
Once the library has been included we can initialize the SmartWall on your website.
Initialization
Once your SmartWall is properly set up in the dashboard you must initialize it as shown below.
<?php
$smartWallSdk = new SmartWall();
$smartWallSdk->smartWallId = 'your-smartwall-id';
?>
You can also directly pass the parameters when creating the object.
<?php
$smartWallSdk = new SmartWall(array(
'smartWallId' => 'your-smartwall-id',
));
?>
Secret Key
The SmartWall needs a secret key to protect the content. More complicate your key is, better it is.
<?php
$smartWallSdk->secretKey = 'superC0mplicat3dS3cret-Key!';
?>
Article Identifier
The SmartWall needs a unique identifier for each article to work properly. It can be a string or an integer. To specify, it proceed as described in the example below:
<?php
$smartWallSdk->articleId = '123'; // accept both integer and string
// If you do not have an article ID use the PATH URL instead (not-recommended)
?>
Multi-languages
The SmartWall user interface handles multiple languages. When initializing the SmartWall you can specify the language you want the SmartWall to be displayed with. Currently available languages are:
- English
en
- French
fr
- German
de
- Italian
it
- Spanish
es
- Portuguese
pt
- Japanese
ja
If you want to use a language not yet implemented, please contact us and we will add it for you.
<?php
$smartWallSdk->language = 'fr';
?>
Include necessary scripts
To work properly the SmartWall requires some JavaScript code. To do so you must include them using the getScripts
method.
It is important to either call this method after the show
method or to provide the getScripts
method the number of characters present in your article.
Bellow you can see both methods.
You must provide an Article ID before calling the
getScripts
method
Example calling getScripts
method after show
method
<!DOCTYPE html>
<html>
<body>
<?php echo $smartWallSdk->show($htmlContent); ?>
<!-- your website content here -->
<?php echo $smartWallSdk->getScripts(); ?>
</body>
</html>
Example calling getScripts
method before show
method
<!DOCTYPE html>
<html>
<head>
<?php echo $smartWallSdk->getScripts(strlen($htmlContent)); ?>
</head>
<body>
<?php echo $smartWallSdk->show($htmlContent); ?>
</body>
</html>
Protect your article and display the SmartWall
To display the SmartWall you only need to call the show
method. Pass it the html content of your article and it will truncate it and hide what is necessary. It will show the SmartWall right after the truncated content.
You must provide an Article ID before calling the
show
method
<?php
// assume your entire article content is in a variable named $htmlContent
$htmlContent = <<<EOD
<div id="article">
<p>
Lorem ipsum dolor sit amet...
</p>
</div>
EOD;
echo $smartWallSdk->show($htmlContent);
?>
Provide an endpoint
The SmartWall will have to contact your backend in order to pass user tokens and communicate with the SmartWall API. This requires two things:
Define an endpoint
Create an endpoint and provide the URL to the SmartWall
<?php
$smartWallSdk->endpoint = 'https://your-website.com/endpoints/smartwall';
?>
Code the endpoint
Once your endpoint is created you must call the following method from it
<?php
$smartWallSdk->apiProxy();
?>
Custom Metrics
You can send your own metrics and value to the SmartWall system. You will be able to use them to sort your audience and optimize your results. In order to send metrics you need to use the following code:
<?php
new SmartWallMetric(array(
'name' => 'name-of-your-metric',
'value' => 3 // optional, by default the value is set to 1
));
?>
Custom Variables
Unlike the custom metrics, custom variables are not time based. They are set during the initialization of the SmartWall with a fixed value. You will be able to use it to sort your audience and optimize your results. In order to use it use the following code:
<?php
// custom variables can be of type: boolean, string and integer
$smartWallSdk->customVariables = array(
'whatever' => true,
'something' => 123,
'anything' => 'whatever'
);
?>