handsetdetection/php-apikit

此包已被废弃,不再维护。未建议替代包。

一个用于独立或Web服务设备检测的库,使用HandsetDetection.com。

4.1.13 2017-09-21 07:48 UTC

README

Build Status Latest Stable Version License

PHP API Kit v4.0,实现HandsetDetection API的v4.0。

API Kits可以使用我们的Web服务或根据您的配置本地解析检测。

安装

直接从github下载包或使用composer。

{
	"require": {
	  "handsetdetection/php-apikit": "4.*"
	}
}

配置

API Kit配置文件可以直接从Handset Detection下载。

  1. 登录到您的仪表板
  2. 点击“添加网站”
  3. 配置您的新网站
  4. 获取API Kit的配置文件变量(从网站设置中)
  5. 将变量放入hdconfig.php文件中

示例

实例化HD4对象

// Using the default config file
require_once('HD4.php');
$hd = new HandsetDetection\HD4();

或者

// Using a custom config file
require_once('HD4.php');
$hd = new HandsetDetection\HD4('/tmp/myCustomConfigFile.php');

列出所有厂商

if ($hd->deviceVendors()) {
    $data = $hd->getReply();
    print_r($data);
} else {
    print $hd->getError();
}

列出某个厂商的所有设备型号(诺基亚)

if ($hd->deviceModels('Nokia')) {
    $data = $hd->getReply();
    print_r($data);
} else {
    print $hd->getError();
}

查看特定设备的信息(诺基亚N95)

if ($hd->deviceView('Nokia','N95')) {
    $data = $hd->getReply();
    print_r($data);
} else {
    print $hd->getError();
}

哪些设备具有此属性?

if ($hd->deviceWhatHas('network','CDMA')) {
    $data = $hd->getReply();
    print_r($data);
} else {
    print $hd->getError();
}

基本设备检测

这是最简单的检测调用 - 自动获取http头。

if ($hd->deviceDetect()) {
    $tmp = $hd->getReply();
    print_r($tmp);
} else {
    print $hd->getError();
}

手动设置http头(user-agent等)

$hd->setDetectVar('user-agent','Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95-3/20.2.011 Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413');
$hd->setDetectVar('x-wap-profile','http://nds1.nds.nokia.com/uaprof/NN95-1r100.xml');
if ($hd->deviceDetect()) {
    $tmp = $hd->getReply();
    print_r($tmp);
} else {
    print $hd->getError();
}

下载完整终极版

注意:在下载存档之前,增加默认超时时间。

$hd->setTimeout(500);
if ($hd->deviceFetchArchive()) {
    $data = $hd->getRawReply();
    echo "Downloaded ".strlen($data)." bytes";
} else {
    print $hd->getError();
    print $hd->getRawReply();
}

下载社区终极版

$hd->setTimeout(500);
if ($hd->communityFetchArchive()) {
    $data = $hd->getRawReply();
    echo "Downloaded ".strlen($data)." bytes";
} else {
    print $hd->getError();
    print $hd->getRawReply();
}

灵活的缓存选项

版本4.1.*包括APC(u)、Memcache、Memcached和Redis缓存选项。为了向后兼容,如果配置文件中未设置任何选项,则默认为APC。

注意:MemcachedMemcached都有一个默认的最大对象大小为1Mb,这太低。我们建议将此限制至少增加到5Mb,使用-I或--max-item-size=选项。

使用Memcache

请将以下缓存配置包含在您的配置文件中。选项代表您希望传递给memcache::set调用的任何缓存标志。更多信息请参见https://php.ac.cn/manual/en/memcache.set.php

$hdconfig['cache'] = array (
	'memcache' => array (
		'options' => 0,
		'servers' => array(
			'localhost' => '11211'
		)
	)
)

使用Memcached

请将以下缓存配置包含在您的配置文件中。选项代表通过setOption调用设置的缓存设置:更多信息请参见https://php.ac.cn/manual/en/memcached.setoption.php。在选项数组中以$option => $value的形式传递选项。

$hdconfig['cache'] = array (
	'memcached' => array(
		'options' => array(),
		'servers' => array(
			array('localhost', '11211'),
		)
	)
);

如果您使用缓存连接池,则按以下方式传递池名称

$hdconfig['cache'] = array (
	'memcached' => array(
		'pool' => 'mypool',
		'options' => array(),
		'servers' => array(
			array('localhost', '11211'),
		)
	)
);

使用Predis

从版本4.1.11起,我们也有Redis作为缓存选项。Redis缓存使用Predis,您应该通过composer包含它。使用以下缓存配置

$hdconfig['cache'] = array (
	'redis' => array (
		'scheme' => 'tcp',
		'host'   => '127.0.0.1',
		'port'   => 6379
	)
);

使用PHPRedis

从版本4.1.12起,我们也有PhpRedis作为缓存选项,它使用来自https://github.com/phpredis/phpredis的redis.so扩展。connect_method可以是connect或pconnect。pconnect连接也支持持久_id。

使用以下缓存配置

$hdconfig['cache'] = array (
	'phpredis' => array (
		'connect_method' => 'pconnect',
		'host'   => '127.0.0.1',
		'port'   => 6379,
		'timeout' => 2.5,
		'persistent_id' => 'x'
	)
);

其他示例

更多示例可以在examples.php文件中找到。

免费使用层和社区版入门

注册我们的服务后,您将进入免费使用层,这使您每月有20,000次云检测(网络服务)的资格,并可以访问我们的社区版 Ultimate(独立)检测。独立检测的存档可以手动下载,但 easiest是配置API套件,并使用您的凭据,让API套件为您完成繁重的工作。请参见上面的示例了解如何操作。

有关手动安装存档的说明可在v4 API Ultimate Community Edition, Getting Started找到。

单元测试

单元测试使用phpUnit,可以在tests目录中找到。

API文档

请参阅v4 API文档

API套件

请参阅Handset Detection GitHub仓库

支持

如果您有任何问题,请告诉我们(hello@handsetdetection.com)。