Here's the complete code for reading through SDF:
<?php
session_start();
require_once '<your path>/common.php';
try{
$args = ($_SERVER['REQUEST_METHOD'] == 'POST')? $_POST : $_GET;
$sessionId = $args['SESSION'];
$mapName = $args['MAPNAME'];
MgInitializeWebTier ($webconfigFilePath);
$userInfo = new MgUserInformation($sessionId);
$siteConnection = new MgSiteConnection();
$siteConnection->Open($userInfo);
try{
$resourceService = $siteConnection->CreateService(MgServiceType::ResourceService);
$featureService = $siteConnection->CreateService(MgServiceType::FeatureService);
$map = new MgMap();
$map->Open($resourceService, $mapName);
$selection = new MgSelection($map, $_POST['SELECTION']);
$layers = $selection->GetLayers();
if($layers->GetCount() > 0){
$layer = $layers->GetItem(0);
if($layer){
$layerClassName = $layer->GetFeatureClassName();
$selectionString = $selection->GenerateFilter($layer,$layerClassName);
$layerFeatureId = $layer->GetFeatureSourceId();
// Collect objects to be passed to SelectFeatures function
$layerFeatureResource = new MgResourceIdentifier($layerFeatureId);
$queryOptions = new MgFeatureQueryOptions();
$queryOptions->AddFeatureProperty('Field 1');
$queryOptions->AddFeatureProperty('Field 2');
$queryOptions->SetFilter($selectionString);
try{
// Create the reader
$featureReader = $featureService->SelectFeatures($layerFeatureResource,$layerClassName, $queryOptions);
$featureReader->ReadNext();
$field1= $featureReader->GetString('Field 1');
$field2 = $featureReader->GetString('Field 2');
echo $field1 .' - '.$field2;
}
catch(MgApplicationException $ex){
echo '<b>Exception: </b>'.$ex->GetDetails();
}
}
}
}
catch (MgException $e){
echo $e->GetExceptionMessage();
echo $e->GetDetails();
}
}
catch (MgException $e){
echo $e->GetExceptionMessage();
echo $e->GetDetails();
}
?>
Note that the value $_POST['SELECTION']
was sent via a hidden field:
<input type="hidden" name="SELECTION" id="hidSelectionXML" />
And it was populated by this line in JavaScript, before the form was redirected to this code:
document.getElementById('hidSelectionXML').value = parent.parent.mapFrame.GetSelectionXML();
For this code to work, you should have selected feature (point, line, circle, etc.) on the map.
Hope this helps someone else's hours of frustration.