This example shows how to get the device detection engine to refresh its internal data structures when a new data file is available.This can be done asynchronously, without the need to restart the machine.
This example is available in full on GitHub.
In this example we create an on premise 51Degrees device detection pipeline. In order to do this you will need a copy of the 51Degrees on-premise library and need to make the following additions to your php.ini file
When running under process manager such as Apache MPM or php-fpm, make sure to set performance_profile to MaxPerformance by making the following addition to php.ini. More details can be found in README.
Under process managers, refreshing internal data will not work as it is required to be performed on both main process and child processes. There is not a proven solution to do so yet, so we recommend a full server restart to be performed instead.
Expected output:
Is User-Agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' a mobile?
true
Is User-Agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36' a mobile?
false
Reloading data file...
Is User-Agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' a mobile?
true
Is User-Agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36' a mobile?
false
<?php
require __DIR__ . '/../../vendor/autoload.php';
use fiftyone\pipeline\core\PipelineBuilder;
$deviceEngine = new DeviceDetectionOnPremise();
$pipeline = new PipelineBuilder();
$pipeline = $pipeline->add($deviceEngine)->build();
function manualDataUpdate_checkifmobile($pipeline, $userAgent = '')
{
$flowData = $pipeline->createFlowData();
$flowData->evidence->set('header.user-agent', $userAgent);
$result = $flowData->process();
echo "Is User-Agent '<b>" . $userAgent . "</b>' a mobile device?:";
echo "</br>\n";
if ($result->device->ismobile->hasValue) {
if ($result->device->ismobile->value) {
echo 'Yes';
} else {
echo 'No';
}
} else {
echo $result->device->ismobile->noValueMessage;
}
echo "</br>\n";
echo "</br>\n";
}
$desktopUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36';
$iPhoneUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
manualDataUpdate_checkifmobile($pipeline, $desktopUA);
manualDataUpdate_checkifmobile($pipeline, $iPhoneUA);
echo 'Reloading data file...';
$deviceEngine->refreshData();
manualDataUpdate_checkifmobile($pipeline, $desktopUA);
manualDataUpdate_checkifmobile($pipeline, $iPhoneUA);