<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Adjust the width and height as needed. This assumes that the map name is 'Layers' which I've seen is true in all the services I tried. If you were using a proper SOAP call you might try GetDefaultMapName first to make sure you have the right map name.
<soapenv:Body>
<GetLegendInfo xmlns="http://www.esri.com/schemas/ArcGIS/9.3">
<MapName>Layers</MapName>
<LayerIDs/>
<LegendPatch>
<Width>10.0</Width><Height>10.0</Height>
<ImageDPI>96.0</ImageDPI>
</LegendPatch>
<ImageType>
<ImageFormat>esriImagePNG</ImageFormat>
<ImageReturnType>esriImageReturnMimeData</ImageReturnType>
</ImageType>
</GetLegendInfo>
</soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.esri.com/schemas/ArcGIS/9.3">
<soap:Body>
<tns:GetLegendInfoResponse>
<Result xsi:type="tns:ArrayOfMapServerLegendInfo">
<MapServerLegendInfo xsi:type="tns:MapServerLegendInfo">
<LayerID>1</LayerID>
<Name>Block Groups</Name>
<LegendGroups xsi:type="tns:ArrayOfMapServerLegendGroup">
<MapServerLegendGroup xsi:type="tns:MapServerLegendGroup">
<Heading>2009 Dominant Tapestry Code and Segment Name</Heading>
<LegendClasses xsi:type="tns:ArrayOfMapServerLegendClass">
<MapServerLegendClass xsi:type="tns:MapServerLegendClass">
<Label>1, Top Rung</Label>
<Description></Description>
<SymbolImage xsi:type="tns:ImageResult">
<ImageData>iVBORw0KGgoAAAANSUhEUgAAABEAAAAOBAMAAAA7w+qHAAAACVBMVEXQt9r+/v7+///7vZCoAAAAA3RSTlP//wDXyg1BAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAJUlEQVQImWNQggIFBiVBMBBSYFCE
sASBLAEGEKAeC2EynAV3AQBHrQhTtwitygAAAABJRU5ErkJggg==
</ImageData>
Each layer name is in the name tag and each symbol for that layer is under the label tag. There may be several label tags for each layer. The tag ImageData contains the image you need to save. If your requested ImageReturnType was esriImageReturnMimeData you get back the raw data for the legend images. The tricky part is converting the legend images, which are output in base64, to regular binary code and saving that into PNG files. Perl handles the decoding very easily with the MIME::Base64 module. If the requested ImageReturnType is esriImageReturnURL the legend images are output on the server and you get back a URL.
use strict;
use LWP::UserAgent;
use MIME::Base64;#GetLegendInfo
my $xmlRequestString; #set this to the above SOAP XML message
my $server = "server.arcgisonline.com";my $service = "Demographics/USA_Tapestry";
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $url ="http://$server/ArcGIS/services/$service/MapServer";
my $req = HTTP::Request->new('POST', $url, HTTP::Headers->new('Content-Type' => 'text/xml', 'SOAPAction' => ""), $xmlRequestString);
my $response = $ua->request($req);
my $content = $response->content;my $i =0;
foreach my $layer ($content =~ m/<MapServerLegendInfo(.+?)<\/MapServerLegendInfo/sg){
my ($layerName) = $layer =~ m/<Name>(.+?)<\/Name>/;
print "Layer: $layerName \n";
while ($layer =~ m/<Label>(.*?)<\/Label.*?<ImageData>(.+?)<\/ImageData/sg){
print " $i $1 \n";
open PNG, ">$i.png" || die "Can't open file $!";
binmode PNG;
print PNG decode_base64($2);
close PNG;
$i++;
}
}
die "Error: ", $response->status_line unless $response->is_success;
while ($layer =~ m/
PS: The ESRI Code Gallery now features code to get the legends. They're using JAVA and proper calls to the SOAP API. It's not as simple as my one script example, but check it out also: http://resources.esri.com/arcgisserver/apis/javascript/arcgis/index.cfm?fa=codeGalleryDetails&scriptID=16096