This example demonstrates how to get location information from the device
that a user is using to access a website.
In addition, JavaScript is used to update the page with postal address details
for this location.
<?php
require(__DIR__ . "/../../vendor/autoload.php");
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/v399y42f' . '<br/>';
$message .= 'Once complete, populate the environment variable ';
$message .= 'mentioned at the start of this message with the key.' . '<br/>';
$message .= "Make sure to include the Country, State, County, Town and ";
$message .= "JavaScript properties as they are used by this example.<br />";
echo $message;
return;
}
$javascriptBuilderSettings = array(
"endpoint" => "/?json"
);
$builder = new GeoLocationPipelineBuilder(array(
"resourceKey" => $resourceKey,
"locationProvider" => "fiftyonedegrees",
"javascriptBuilderSettings" => $javascriptBuilderSettings
));
$serializedPipelineFile = __DIR__ . "web_integration_pipeline.pipeline";
if(!file_exists($serializedPipelineFile)){
$pipeline = $builder->build();
file_put_contents($serializedPipelineFile, serialize($pipeline));
} else {
$pipeline = unserialize(file_get_contents($serializedPipelineFile));
}
$flowData = $pipeline->createFlowData();
$flowData->evidence->setFromWebRequest();
$result = $flowData->process();
if (isset($_GET["json"])) {
header('Content-Type: application/json');
echo json_encode($flowData->jsonbundler->json);
return;
}
$properties = $pipeline->getElement("location")->getProperties();
if (!isset($properties["country"]) || !isset($properties["state"]) ||
!isset($properties["county"]) || !isset($properties["town"]) ||
!isset($properties["javascript"])) {
echo "Make sure to include the Country, State, County, Town and " .
"JavaScript properties as they are used by this example.\n<br />";
return;
}
echo "<p> The following values are determined sever-side on the first request.
As the server has no location information to work from,
these values will all be unknown:</p>";
echo "<dl>";
echo "<dt>";
echo "<strong>Country</strong>";
echo "</dt>";
echo "<dd>";
if ($result->location->country->hasValue) {
echo $result->location->country->value;
} else {
echo "Unknown (" . $result->location->country->noValueMessage . ")";
}
echo "</dd>";
echo "<dt>";
echo "<strong>State</strong>";
echo "</dt>";
echo "<dd>";
if ($result->location->state->hasValue) {
echo $result->location->state->value;
} else {
echo "Unknown (" . $result->location->state->noValueMessage . ")";
}
echo "</dd>";
echo "<dt>";
echo "<strong>County</strong>";
echo "</dt>";
echo "<dd>";
if ($result->location->county->hasValue) {
echo $result->location->county->value;
} else {
echo "Unknown (" . $result->location->county->noValueMessage . ")";
}
echo "</dd>";
echo "<dt>";
echo "<strong>Town/City</strong>";
echo "</dt>";
echo "<dd>";
if ($result->location->town->hasValue) {
echo $result->location->town->value;
} else {
echo "Unknown (" . $result->location->town->noValueMessage . ")";
}
echo "</dd>";
echo "</dl>";
echo "
<p>
When the button below is clicked, JavaScript running on the client-side
will be used to obtain additional evidence (i.e. the location information
from the device). If no additional information appears then it may
indicate an external problem such as JavaScript being disabled in
your browser.
</p>
<p>
Note that the accuracy of the information is dependent on the accuracy
of the location data returned by your device. Any device that lacks GPS
is likely to return a highly inaccurate result. Among devices with GPS,
some have a significantly lower margin of error than others.
</p>
<button type='button' onclick='buttonClicked()'>Use my location</button>";
echo "<dl>";
echo "<dt>";
echo "<strong>Country</strong>";
echo "</dt>";
echo "<dd id='countryclient'>";
echo "</dd>";
echo "<dt>";
echo "<strong>State</strong>";
echo "</dt>";
echo "<dd id='stateclient'>";
echo "</dd>";
echo "<dt>";
echo "<strong>County</strong>";
echo "</dt>";
echo "<dd id='countyclient'>";
echo "</dd>";
echo "<dt>";
echo "<strong>Town/City</strong>";
echo "</dt>";
echo "<dd id='townclient'>";
echo "</dd>";
echo "<script>" . $flowData->javascriptbuilder->javascript . "</script>";
?>
<!--
Now we add some additional JavaScript
that will update the client side properties above if values exist for them
in the JSON endpoint provided data
-->
<script>
buttonClicked = function () {
fod.complete(function (data) {
if(data.location){
document.getElementById('countryclient').innerHTML = data.location.country;
document.getElementById('stateclient').innerHTML = data.location.state;
document.getElementById('countyclient').innerHTML = data.location.county;
document.getElementById('townclient').innerHTML = data.location.town;
} else {
document.getElementById('countryclient').innerHTML = 'Location data is empty. This probably means that something has gone wrong with the JavaScript evaluation.';
}
}, 'location');
}
</script>