This example shows how the hasValue function can help make sure that meaningful values * are returned when checking properties returned from the device detection engine.
This example shows how the hasValue function can help make sure that meaningful values * are returned when checking properties returned from the device detection engine. This example is available in full on GitHub.
Does User-Agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' represent a mobile device?:
Yes
Does User-Agent 'xyfga' represent a mobile device?:
We don't know for sure. The reason given is:
No matching entries could be found for the supplied evidence.
This is because the supplied User-Agent cannot be mapped to any known device.
<?php
require(__DIR__ . "/../../vendor/autoload.php");
$resourceKey = ExampleUtils::getResourceKeyFromEnv();
if (empty($resourceKey)) {
$resourceKey = ExampleUtils::getResourceKeyFromQueryParameter();
}
if (empty($resourceKey)) {
$message = 'No resource key specified in the environment variable or query parameter ';
$message .= "'" . ExampleUtils::RESOURCE_KEY_ENV_VAR . "'." . PHP_EOL;
$message .= 'Create a resource key with the properties required by this example';
$message .= 'at https://configure.51degrees.com?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-cloud-failuretomatch.php&utm_term=resource-key-required' . '<br/>';
$message .= 'Once complete, populate the environment variable or query parameter ';
$message .= 'mentioned at the start of this message with the key.' . '<br/>';
ExampleUtils::logErrorAndExit(new Logger('info'), $message);
}
$builder = new DeviceDetectionPipelineBuilder(array(
"resourceKey" => $resourceKey,
"restrictedProperties" => array()
));
$pipeline = $builder->build();
function failuretomatch_checkifmobile($pipeline, $userAgent = "")
{
$flowData = $pipeline->createFlowData();
$flowData->evidence->set("header.user-agent", $userAgent);
$result = $flowData->process();
print("Does User-Agent '<b>" . $userAgent . "</b>' represent a mobile device?:");
print("</br>\n");
if ($result->device->ismobile->hasValue) {
if ($result->device->ismobile->value) {
print("Yes");
} else {
print("No");
}
} else {
print("We don't know for sure. The reason given is:");
print("</br>\n");
print($result->device->ismobile->noValueMessage);
print("</br>\n");
print("This is because the supplied User-Agent cannot be " .
"mapped to any known device.");
}
print("</br>\n");
print("</br>\n");
}
$iPhoneUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
failuretomatch_checkifmobile($pipeline, $iPhoneUA);
$badUserAgent = 'xyfga';
failuretomatch_checkifmobile($pipeline, $badUserAgent);
Definition ExampleUtils.php:29