-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.php
148 lines (115 loc) · 4.81 KB
/
web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
Copyright 2019 ScientiaMobile Inc. http://www.scientiamobile.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require_once __DIR__ . '/vendor/autoload.php';
if (version_compare(PHP_VERSION, "7.1", "<")) {
echo "This example requires PHP >= 7.1";
exit;
}
if (!class_exists("\Laminas\Diactoros\ServerRequestFactory")) {
echo "This example uses the Laminas\Diactoros library. Please install with: composer require laminas/laminas-diactoros";
}
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: <br>";
echo "<ul>";
echo "<li>WM version: " . $serveInfo->wmVersion() . "</li>";
echo "<li>WURFL API version: " . $serveInfo->wurflAPIVersion() . "</li>";
echo "<li>WURFL file info: " . $serveInfo->wurflInfo() . "</li>";
echo "</ul>";
// 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"]);
// Fetch the server request
// Note: In this example we'll using Laminas Diactoros to create a PSR7 request
// from the supplied superglobal values and requires.
// Laminas Diactoros requires PHP >= 7.1
// The library can be installed via composer:
// composer require laminas/laminas-diactoros
$serverRequest = \Laminas\Diactoros\ServerRequestFactory::fromGlobals();
// Perform a device detection calling WM server API
$deviceData = $wmClient->lookupRequest($serverRequest);
if ($deviceData->error()) {
echo "WM client returned an error: " . $deviceData->error() . "<br>";
exit;
}
// Let's get the device capabilities and print some of them
echo "WURFL device id: " . $deviceData->capabilities("wurfl_id") . "<br>";
// print brand & model (static capabilities)
echo "This device is a: " . $deviceData->capabilities("brand_name") . " " . $deviceData->capabilities("model_name") . "<br>";
// check if device is a smartphone (a virtual capability)
if ($deviceData->capabilities("is_smartphone") === "true") {
echo "This is a smartphone" . "<br>";
} else {
echo "This is not a smartphone" . "<br>";
}
// Printing all received capabilities
echo "<br>All received capabilities: <br>";
echo "<ul>";
foreach ($deviceData->getAllCapabilities() as $key => $value) {
echo "<li>$key: $value</li>";
}
echo "</ul>";
// Get all the device manufacturers, and print the first twenty
$limit = 20;
$deviceMakes = $wmClient->getAllDeviceMakes();
echo "Print the first $limit Brand of " . count($deviceMakes) . "<br>";
// Sort the device manufacturer names
sort($deviceMakes);
echo "<ul>";
for ($i = 0; $i < $limit; $i++) {
echo "<li>" . $deviceMakes[$i] . "</li>";
}
echo "</ul>";
// Now call the WM server to get all device model and marketing names produced by Apple
echo "Print all Model for the Apple Brand" . "<br>";
$modelMktNames = $wmClient->getAllDevicesForMake("Apple");
// Sort $modelMktNames by their model name
array_multisort($modelMktNames);
echo "<ul>";
foreach ($modelMktNames as $modelMktName) {
echo "<li>" . $modelMktName->modelName() . " " . $modelMktName->marketingName() . "</li>";
}
echo "</ul>";
// Now call the WM server to get all operative system names
$oses = $wmClient->getAllOSes();
echo "Print the list of OSes" . "<br>";
// Sort and print all OS names
sort($oses);
echo "<ul>";
foreach ($oses as $os) {
echo "<li>" . $os . "</li>";
}
echo "</ul>";
// Let's call the WM server to get all version of the Android OS
echo "Print all versions for the Android OS" . "<br>";
$versions = $wmClient->getAllVersionsForOS("Android");
// Sort all Android version numbers and print them.
sort($versions);
echo "<ul>";
foreach ($versions as $version) {
echo "<li>" . $version . "</li>";
}
echo "</ul>";