PHP code example of chartbeat / chartbeat-php

1. Go to this page and download the library: Download chartbeat/chartbeat-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chartbeat / chartbeat-php example snippets


{
	"chartbeat/chartbeat-php" : "1.*"
	}
}


$apiKey = '<your api key>';
$host = '<your host>';
$cb = new Chartbeat\Chartbeat($host, $apiKey);

// get monthly max people on site
echo 'Monthly max people on the site: ' . $cb->getMonthlyMaxPeople(); 

$cbData = $cb->get('live/toppages/v3/', array('limit' => 1)); // get all top pages, we want only 1
$topPage = current($cbData->pages); // get first page of the result
var_dump(current($topPage->stats->toprefs)); // output first top referer of page

// outputs:
object(stdClass)#9 (2) {
  ["visitors"]=>
  int(10)
  ["domain"]=>
  string(10) "google.com"
}

try {
	// set invalid key and try to get data
	$cb->setApiKey('invalidapikey');
	$cb->getMonthlyMaxPeople(); 
} catch (Chartbeat\Exception $ex)
{
	echo "Caught exception: " . $ex->getMessage();
	// will output "Caught exception: Chartbeat API error. Message: No Access, Code: 403"
}