Anura SDK for PHP
The Anura SDK for PHP makes it easy for developers to access Anura Direct within their PHP code, and begin analyzing their traffic. You can get started in minutes by installing the SDK with Composer, or by downloading a single zip or phar file from our latest release.
Getting Started
- Have an open active account with Anura - You can see more about Anura's offerings here.
- Minimum Requirements - To use the SDK, you will need PHP >= 7.4
-
Install the SDK - Using Composer is the easiest and recommended way to install it. The SDK is available on Packaigst. If Composer is installed globally on your system, you can run the following command in the base directory of your project to add the SDK as a dependency:
composer require anura/anura-sdk-php
If you're looking to install the SDK without Composer, see the Alternative Installation Methods section below.
- Using the SDK - The best way to start getting familiar with the SDK is to have a look at our Quick Examples section which covers the most common use-cases for it.
Alternative Installation Methods
Installing via PHAR
We also have a PHAR on within our GitHub repository if you'd like to install it that way. Here's how to do it:
- Download the PHAR from our latest release, and place it within your project.
- Require or include the anura-sdk-php.phar file within your project.
- You're good to go!
Installing via ZIP Archive
Lastly, we have the SDK bundled within a ZIP archive for you to add to your project. Here's how to install it:
- Download anura-sdk-php.zip from our latest release, extract it, and place anura-sdk-php within your project.
- Require or include our autoloader at anura-sdk-php/autoload.php
- You're good to go!
Quick Examples
Create the Anura Direct client
<?php
// Make sure you require the Composer autoloader!
require 'vendor/autoload.php';
// Or if you're using the PHAR, require/include that.
require '/path/to/anura-sdk-php.phar';
// Or if you're using the ZIP, require/include its autoloader.
require '/path/to/anura-sdk-php/autoload.php';
use Anura\AnuraDirect;
use Anura\DirectResult;
// Creating an Anura Direct Client. Replace 'your-instance-id' with your actual Instance ID.
$direct = new AnuraDirect('your-instance-id');
?>
Set a custom source, campaign, and additional data for Anura Direct
$direct->setSource('your-source-value');
$direct->setCampaign('your-campaign-value');
$direct->addAdditionalData('1', 'your-data-value');
Updating additional data at a specific index
// To update an element of additional data, simply add the element again but with a new value.
$indexToUpdate = '1';
$direct->addAdditionalData($indexToUpdate, 'your-new-data-value');
Removing an element from additional data
$indexToRemove = '1';
$direct->removeAdditionalData($indexToRemove);
Get a result from Anura Direct
$result = $direct->getResult(
'visitors-ip-address',
'visitors-user-agent', // optional
'visitors-app-package-id', // optional
'visitors-device-id' // optional
);
if ($result) {
// We got a result (a DirectResult object)!
// See below for available properties & DirectResults objects.
// $result->isSuspect();
// $result->isNonSuspect();
// $result->isMobile();
// $anuraResult = $result->result;
// $visitorWasMobile = $result->mobile;
// $ruleSets = $result->ruleSets;
// $invalidTrafficType = $result->invalidTrafficType;
} else {
// An error occurred. Can retrieve the error message using getError().
$error = $direct->getError();
// Error handling logic goes here.
}
API Reference
AnuraDirect
Can get results from Anura Direct. These results are fetched using Direct's /direct.json API endpoint.
Methods
getResult(): ?DirectResult- Gets a result from Anura Direct. Returns null if an error was received from Anura Direct. The error can be received by calling getError().
Parameters:
Name | Type | Description | Required |
---|---|---|---|
$ipAddress | string | The IP address of your visitor. Both IPv4 & IPv6 addresses are supported. | Yes |
$userAgent | string | The user agent string of your visitor. | |
$app | string | The application package identifier of your visitor (when available.) | |
$device | string | The device identifier of your visitor (when available.) |
getError(): ?string
- Returns the last received error from Anura Direct. Returns null if an error has not occurred.
getInstance(): string
- Returns the instance you have set within the AnuraDirect client.
getSource(): string
- Returns the source you have set within the AnuraDirect client.
getCampaign(): string
- Returns the campaign you have set within the AnuraDirect client.
getAdditionalData(): array
- Returns the additional data you have set within the AnuraDirect client.
setInstance(string $instance): void
- Sets the instance ID of the AnuraDirect client to the $instance value passed.
setSource(string $source): void
- Sets the source of the AnuraDirect client to the $source value passed.
setCampaign(string $campaign): void
- Sets the campaign of the AnuraDirect client to the $campaign value passed.
addAdditionalData(string $key, string $value): void
- Adds an element of additional data to be sent to Anura Direct.
- If you call addAdditionalData() multiple times with the same $key, the element at $key will simply be updated with the new $value.
DirectResult
The result upon a successful call to getResult() from the AnuraDirect client. It contains not only the result from Anura Direct, but some other mehtods to help you use the result.
Methods
isSuspect(): bool
- Returns whether or not the visitor has been determined to be suspect.
isNonSuspect(): bool
- Returns whether or not the visitor has been determined to be non-suspect.
isMobile(): bool
- Returns whether or not the visitor has been determined to be on a mobile device.
Properties
result: string
- Besides using our isSuspect() or isNonSuspect() methods, you are also able to access the result value.
ruleSets: ?array
- If you have return rule sets enabled, you will be able to see which specific rules were violated upon a suspect result. This value will be null if the visitor is non-suspect, or if you do not have return rule sets enabled.
- You can talk to support about enabling or disabling the return rule sets feature.
invalidTrafficType: ?string
- If you have return invalid traffic type enabled, you will be able to see which type of invalid traffic occurred upon a suspect result.
- You can talk to support about enabling or disabling the return invalid traffic type feature.