Skip to content

Examples

Note

This section is under development. If you want some specific example for using the api, please join our slack and ask us.

Monitor

Create new Monitors

<?php
$token = "MY_TOKEN";

echo "Creating monitors ... ";

$payload = json_encode([
    'website' => 'my-website.com',
    'country' => 'en',
    'language' => 'en',
    'keywords' => ['my keyword 1', 'my keyword 2']
]);

$context = stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' =>
            'Authorization: Bearer '.$token."\r\n".
            'Content-Type: application/json'."\r\n".
            'Content-Length: '.strlen($payload),
        'content' => $payload
    ]
]);

$response = file_get_contents('https://api.check-position.com/monitors', false, $context);
$result = json_decode($response)->data;

echo "Success, created monitors :".PHP_EOL;
print_r($result);

Get today positions for all Monitors

<?php
$token = "MY_TOKEN";

/*
 * Retrieve the logged account
 */
function getAccount($token)
{
    echo "Getting current logged account ... ";

    $context = stream_context_create([
        'http' => [
            'method' => 'GET',
            'header' => 'Authorization: Bearer '.$token."\r\n"
        ]
    ]);

    $response = file_get_contents('https://api.check-position.com/account', false, $context);
    $result = json_decode($response)->data;

    echo "got account with id " . $result->id . " !" . PHP_EOL;

    return $result;
}

/*
 * Get all the Monitors entities
 */
function getMonitors($token)
{
    echo "Getting all monitors ... ";

    $context = stream_context_create([
        'http' => [
            'method' => 'GET',
            'header' => 'Authorization: Bearer '.$token."\r\n"
        ]
    ]);

    $query = [
        'with_keyword' => true,
        'with_website' => true
    ];

    $response = file_get_contents('https://api.check-position.com/monitors?'.http_build_query($query), false, $context);
    $result = json_decode($response)->data;

    echo "got " . count($result) . " monitors !" . PHP_EOL;

    return $result;
}

/*
 * Get the Monitor position report of the given day
 */
function getMonitorReport($account, $year, $month, $day)
{
    echo "Getting the monitor report of $year-$month-$day ... ";

    $response = file_get_contents('https://storage.check-position.com/monitor/report/'.$year.'/'.$month.'/'.$day.'/'.$account->hid.'.json');
    $result = json_decode($response, true);

    echo "got " . count($result['monitors']) . " positions !" . PHP_EOL;

    return $result;
}

$account = getAccount($token);
$monitors = getMonitors($token);
$reports = getMonitorReport($account, date('Y'), date('m'), date('d'));

// For each monitors, print a line and try to display the position from the report
foreach ($monitors as $monitor) {
    echo $monitor->website->domain.' - ';
    echo $monitor->keyword->term.' ('.$monitor->keyword->language.', '.$monitor->keyword->country.') : ';
    if (key_exists($monitor->id, $reports['monitors'])) {
        $position = $reports['monitors'][$monitor->id][0];

        if ($position === null) {
            echo 'In process ...';
        } elseif ($position === false) {
            echo 'In error.';
        } elseif ($position === 0) {
            echo 'Not found (+100)';
        } else {
            echo 'Found ' . $position;
        }
    } else {
        echo 'No data';
    }

    echo PHP_EOL;
}