wurfl / wurfl-api
WURFL,无线通用资源文件(Wireless Universal Resource FiLe),是一个设备描述库(DDR),即一种将HTTP请求头映射到发出请求的HTTP客户端(桌面、移动设备、平板电脑等)配置文件的软件组件
Requires
- php: >=5.3.0
Requires (Dev)
- fabpot/php-cs-fixer: ^1.11
- phpunit/phpunit: ^4.8|^5.2
- squizlabs/php_codesniffer: ^2.5
Suggests
- ext-redis: phpredis extension allows to use the Redis storage
- predis/predis: predis library allows to use the Redis storage
This package is not auto-updated.
Last update: 2024-09-14 17:21:23 UTC
README
许可证
本程序是免费软件:您可以按照自由软件基金会发布的GNU Affero通用公共许可证的条款重新分发和/或修改它,许可证版本为3,或(根据您的选择)许可证的任何后续版本。
请参阅与该软件包一起分发的COPYING文件,以获取适用GNU Affero通用公共许可证的完整文本。
如果您无法遵守AGPL许可证的条款,可以从ScientiaMobile,Inc获取商业许可证,网址为http://www.ScientiaMobile.com/
PHP支持的版本
- PHP >= 5.3.2
入门指南
从wurfl网站下载发布存档并将其解压缩到适合您应用程序的目录中。
要开始使用API,您需要设置一些配置选项。
重要:WURFL API与WURFL.XML文件紧密相关。WURFL.XML的新版本默认情况下与旧版本的API兼容,但反之则不成立。旧版本的WURFL.XML不能保证与新版本的API兼容。让我们重申一下:此API与旧版本的WURFL.XML不兼容。此分布中包含此API可以使用的最旧的WURFL版本。
对于心急如焚的人
请查看examples/demo目录中的配置文件示例。
$wurflDir = dirname(__FILE__) . '/../../../WURFL'; $resourcesDir = dirname(__FILE__) . '/../../resources'; require_once $wurflDir.'/Application.php'; $persistenceDir = $resourcesDir.'/storage/persistence'; $cacheDir = $resourcesDir.'/storage/cache'; // Create WURFL Configuration $wurflConfig = new WURFL_Configuration_InMemoryConfig(); // Set location of the WURFL File $wurflConfig->wurflFile($resourcesDir.'/wurfl.zip'); // Set the match mode for the API ('performance' or 'accuracy') $wurflConfig->matchMode('performance'); // Setup WURFL Persistence $wurflConfig->persistence('file', array('dir' => $persistenceDir)); // Setup Caching $wurflConfig->cache('file', array('dir' => $cacheDir, 'expiration' => 36000)); // Create a WURFL Manager Factory from the WURFL Configuration $wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig); // Create a WURFL Manager /* @var $wurflManager WURFL_WURFLManager */ $wurflManager = $wurflManagerFactory->create();
现在您可以使用一些WURFL_WURFLManager
类方法;
$device = $wurflManager->getDeviceForHttpRequest($_SERVER); $device->getCapability("is_wireless_device"); $device->getVirtualCapability("is_smartphone");
创建配置对象
-
设置主WURFL文件的位置路径(如果您启用了zip扩展,可以使用zip文件)
-
通过指定提供者和初始化提供者所需的额外参数来配置持久化提供者。API支持以下机制
- Memcache (http://uk2.php.net/memcache)
- APC(替代PHP缓存 https://php.ac.cn/apc)
- MySQL
- 内存
- 文件
-
通过指定提供者和初始化提供者所需的额外参数来配置缓存提供者。API支持以下缓存机制
- Memcache (http://uk2.php.net/memcache)
- APC(替代PHP缓存 https://php.ac.cn/apc)
- 文件
- 空(无缓存)
示例配置
// Create WURFL Configuration $wurflConfig = new WURFL_Configuration_InMemoryConfig(); // Set location of the WURFL File $wurflConfig->wurflFile($resourcesDir.'/wurfl.zip'); // Set the match mode for the API ('performance' or 'accuracy') $wurflConfig->matchMode('performance'); // Setup WURFL Persistence $wurflConfig->persistence('file', array('dir' => $persistenceDir)); // Setup Caching $wurflConfig->cache('file', array('dir' => $cacheDir, 'expiration' => 36000));
使用WURFL PHP API
获取设备
您有四种方法来检索设备
-
getDeviceForRequest(WURFL_Request_GenericRequest $request)
,其中WURFL_Request_GenericRequest是一个对象,它封装了设备的使用代理、ua-profile以及设备对xhtml的支持。 -
getDeviceForHttpRequest($_SERVER)
您通常将使用此方法,并且API会为您创建WURFL_Request_GenericRequest对象。 -
getDeviceForUserAgent(string $userAgent)
用于查询API以获取给定的使用代理字符串。 -
getDevice(string $deviceID)
通过设备的设备ID获取设备(例如:apple_iphone_ver1
)。
使用示例
$device = $wurflManager->getDeviceForHttpRequest($_SERVER);
获取设备属性及其功能
设备ID和回退设备ID是设备的属性
$deviceID = $device->id; $fallBack = $device->fallBack;
要获取功能值,请使用 getCapability()
$value = $device->getCapability("is_tablet"); $allCaps = $device->getAllCapabilities();
要获取虚拟功能值,请使用 getVirtualCapability()
$value = $device->getVirtualCapability("is_smartphone"); $allVCaps = $device->getAllVirtualCapabilities();
有用方法
根WURFL设备对象有一些有用的函数
/* @var $device WURFL_CustomDevice */ $device = $wurflManager->getDeviceForHttpRequest($_SERVER); /* @var $root WURFL_Xml_ModelDevice */ $root = $device->getRootDevice(); $group_names = $root->getGroupNames(); $cap_names = $root->getCapNames(); $defined = $root->isCapabilityDefined("foobar");
WURFL重载器
WURFL可以通过检查WURFL文件的修改时间来自动更新持久数据,无需任何配置。要启用,请在配置中将 allow-reload 设置为 true
$wurflConfig->allowReload(true);
如果您有任何问题,请查看 http://wurfl.sourceforge.net 上的文档,以及 http://www.scientiamobile.com/forum 上的ScientiaMobile支持论坛