scientiamobile/wm-client

WURFL 微服务 PHP API

2.0.3 2022-12-05 15:37 UTC

This package is auto-updated.

Last update: 2024-08-29 23:28:27 UTC


README

WURFL 微服务(由ScientiaMobile,Inc.提供)是一种移动设备检测服务,可以快速准确地检测访问设备的500多种功能。它可以区分便携式移动设备、桌面设备、智能电视以及其他任何拥有网络浏览器的设备。

这是访问WURFL微服务的PHP客户端API。该API开源发布,可以与其他开源或专有代码集成。为了运行,它需要访问正在运行的WURFL微服务产品实例,例如

要求

  • PHP 5.5+(建议PHP >= 7.1)
  • json 扩展(通常包含在内)
  • 推荐安装 curl 扩展

使用composer安装

composer require scientiamobile/wm-client

WURFL微服务客户端示例

<?php
require_once '/path/to/vendor/autoload.php';

try {
    // First we need to create a WM client instance, to connect to our WM server API at the specified host and port.
    $wmClient = \ScientiaMobile\WMClient\WMClient::create("http", "localhost", "8080");
    // we are activating the caching option in WM client. In order to not use cache, you just to need to omit enableCache call
    $wmClient->enableCache();
} catch (\Exception $e) {
    // problems such as network errors  or internal server problems
    echo $e->getMessage();
    exit;
}

// We ask Wm server API for some Wm server info such as server API version and info about WURFL API and file used by WM server.
$serveInfo = $wmClient->getInfo();
echo "WM server information: " . PHP_EOL;
echo " - WM version: " . $serveInfo->wmVersion() . PHP_EOL;
echo " - WURFL API version: " . $serveInfo->wurflAPIVersion() . PHP_EOL;
echo " - WURFL file info: " . $serveInfo->wurflInfo() . PHP_EOL;
echo PHP_EOL;

// set the capabilities we want to receive from WM server
// Static capabilities
$wmClient->setRequestedStaticCapabilities(["model_name", "brand_name"]);
// Virtual capabilities
$wmClient->setRequestedVirtualCapabilities(["is_smartphone", "form_factor"]);

$ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3";

// Perform a device detection calling WM server API
$deviceData = $wmClient->lookupUserAgent($ua);

if ($deviceData->error()) {
    echo "WM client returned an error: " . $deviceData->error() . PHP_EOL;
    exit;
}

// Let's get the device capabilities and print some of them
echo "WURFL device id: " . $deviceData->capabilities("wurfl_id") . PHP_EOL;

// print brand & model (static capabilities)
echo "This device is a: " . $deviceData->capabilities("brand_name") . " " . $deviceData->capabilities("model_name") . PHP_EOL;

// check if device is a smartphone (a virtual capability)
if ($deviceData->capabilities("is_smartphone") === "true") {
    echo "This is a smartphone" . PHP_EOL;
} else {
    echo "This is not a smartphone" . PHP_EOL;
}

// Printing all received capabilities
echo PHP_EOL;
echo "All received capabilities:" . PHP_EOL;

foreach ($deviceData->getAllCapabilities() as $key => $value) {
    echo " - $key: $value" . PHP_EOL;
}

// Get all the device manufacturers, and print the first twenty
$limit = 20;

$deviceMakes = $wmClient->getAllDeviceMakes();

echo PHP_EOL;
echo "Print the first $limit Brand of " . count($deviceMakes) . PHP_EOL;

// Sort the device manufacturer names
sort($deviceMakes);

for ($i = 0; $i < $limit; $i++) {
    echo " - " . $deviceMakes[$i] . PHP_EOL;
}

echo PHP_EOL;

// Now call the WM server to get all device model and marketing names produced by Apple
echo "Print all Model for the Apple Brand" . PHP_EOL;

$modelMktNames = $wmClient->getAllDevicesForMake("Apple");

// Sort $modelMktNames by their model name
array_multisort($modelMktNames);

foreach ($modelMktNames as $modelMktName) {
    echo " - " . $modelMktName->modelName() . " " . $modelMktName->marketingName() . PHP_EOL;
}

// Now call the WM server to get all operative system names
$oses = $wmClient->getAllOSes();

echo PHP_EOL;
echo "Print the list of OSes" . PHP_EOL;

// Sort and print all OS names
sort($oses);

foreach ($oses as $os) {
    echo " - " . $os . PHP_EOL;
}

echo PHP_EOL;
// Let's call the WM server to get all version of the Android OS
echo "Print all versions for the Android OS" . PHP_EOL;

$versions = $wmClient->getAllVersionsForOS("Android");

// Sort all Android version numbers and print them.
sort($versions);

foreach ($versions as $version) {
    echo " - " . $version . PHP_EOL;
}

如果您直接克隆了此仓库,您必须在运行上面的示例之前,使用 composer install 首先安装依赖项。

更多示例请参见 示例 文件夹

测试

客户端包含单元测试,可以使用PHPUnit运行。

php vendor/bin/phpunit

请注意,为了使所有测试通过,您需要在 localhost 端口的 8080 上运行Wurfl Microservice Server的实例,否则将跳过集成测试。

2017 ScientiaMobile Incorporation

版权所有。

注意:此处包含的所有信息,以及任何供应商的任何信息,均为ScientiaMobile Incorporation及其供应商的财产。此处包含的知识产权和技术概念均为ScientiaMobile Incorporation及其供应商的专有财产,可能受到美国和外国专利、正在申请的专利以及商业秘密或版权法的保护。除非事先获得ScientiaMobile Incorporation的书面许可,否则严禁传播此信息或复制此材料。