Example of using the 51Degrees geo-location Cloud alongside 51Degrees device detection to determine the country and device for a given longitude, latitude and User-Agent.To run this example you will need to include the Device Detection Cloud engine from the device detection package.
This example uses the 'Country' and 'IsMobile' properties, which are pre-populated when creating a key using the link above.
<?php
require(__DIR__ . "/../../vendor/autoload.php");
use fiftyone\pipeline\devicedetection\DeviceDetectionCloud;
if (isset($_ENV[Constants::RESOURCE_KEY_ENV_VAR])) {
$resourceKey = $_ENV[Constants::RESOURCE_KEY_ENV_VAR];
} else {
$resourceKey = "!!YOUR_RESOURCE_KEY!!";
}
if (substr($resourceKey, 0, 2) === "!!") {
$message = 'No resource key specified in the environment variable ';
$message .= '"' . Constants::RESOURCE_KEY_ENV_VAR . '"' . '<br/>';
$message .= 'Create a resource key with the properties required by this example';
$message .= 'at https://configure.51degrees.com/vHk79wZn' . '<br/>';
$message .= 'Once complete, populate the environment variable ';
$message .= 'mentioned at the start of this message with the key.' . '<br/>';
echo $message;
return;
}
$settings = array(
"resourceKey" => $resourceKey,
"locationProvider" => "fiftyonedegrees"
);
$builder = new GeoLocationPipelineBuilder($settings);
if (!class_exists("fiftyone\pipeline\devicedetection\DeviceDetectionCloud")) {
echo "You will need to include the 51degrees/fiftyone.devicedetection package for this example to run.";
return;
};
$builder->add(new DeviceDetectionCloud());
$pipeline = $builder->build();
$flowData = $pipeline->createFlowData();
$latitude = "51.458048";
$longitude = "-0.9822207999999999";
$flowData->evidence->set('query.51D_Pos_latitude', $latitude);
$flowData->evidence->set('query.51D_Pos_longitude', $longitude);
$userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0";
$flowData->evidence->set('header.user-agent', $userAgent);
$flowData->process();
$country = $flowData->location->country;
if ($country->hasValue) {
echo "Which country is the location [" . $latitude . "," . $longitude . "] in?";
echo "<br />";
echo $country->value;
echo "<br />";
} else {
echo $country->noValueMessage;
echo "<br />";
};
echo "Is user agent '" . $userAgent . "' a mobile device?";
echo "<br />";
if ($flowData->device->ismobile->hasValue) {
if ($flowData->device->ismobile->value) {
print("Yes");
} else {
print("No");
}
} else {
print($flowData->device->ismobile->noValueMessage);
}