chartbeat/chartbeat-php

Chartbeat 的 PHP 库

1.0.0 2013-06-27 20:41 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:01:39 UTC


README

用于与 Chartbeat API 交互的 PHP 库。

请参阅 API 文档API 探索器(来自 Chartbeat 网站)以获取更多信息。

此库需要 PHP >5.3 和 PHP 的 cURL 扩展才能运行。已测试与 PHP 5.3.15 和 5.4.6 一起使用。

请提供反馈,报告错误,分支并贡献。

安装

将 chartbeat-php 添加到您的 composer.json 中

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

简单的测试脚本

require_once ('vendor/autoload.php'); // composer autoloader
$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(); 

您也可以使用 get() 方法进行任何类型的 API 查询。第一个参数是数据端点(例如,'historical/traffic/series'),第二个参数包含参数。

在此处,我们获取当前访问量最大的页面的顶级引用者

$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"
}

错误处理

每个错误都会抛出 Chartbeat\Exception 异常。

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"
}

测试

您可以通过运行 example.php 来测试此库

./example.php <your host> <your API key> 

这将输出您的网站每月的最大人数