我需要能够将一些 XML“发布”到 Geoserver WMS GetMap 并获取图像。 我有一些有效的 XML,我可以使用 Geoserver 上的“演示”功能进行测试,它会按预期返回图像。
但是,我似乎无法弄清楚在 PHP 中生成相同请求和响应所需的机制。
我尝试过使用 cURL(因为我已经成功地用于 WFS 请求)并尝试过 fputs,但我似乎无法破解它。
有人有使用 PHP 向 GeoServer 发送 WMS GetMap“发布”XML 请求的工作示例吗?
非常感谢, 史蒂夫
最佳答案
请参阅下面的代码,了解如何使用curl通过xml、php和curl从geoserver获取项目的示例。
<?php
$xml_builder = '
<?xml version="1.0" encoding="utf-8"?>
<ogc:GetMap xmlns:ogc="http://www.opengis.net/ows"
xmlns:gml="http://www.opengis.net/gml"
version="1.2.0"
service="WMS">
<StyledLayerDescriptor version="1.0.0">
<NamedLayer>
<Name>myNs:roads</Name>
<NamedStyle>
<Name>simple_roads</Name>
</NamedStyle>
</NamedLayer>
</StyledLayerDescriptor>
<Output>
<Format>image/png</Format>
<Size>
<Width>600</Width>
<Height>320</Height>
</Size>
</Output>
<Exceptions>application/vnd.ogc.se+xml</Exceptions>
</ogc:GetMap>
';
// We send XML via CURL using POST with a http header of text/xml.
$ch = curl_init('http://localhost:8080/GeoServer/wms');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
// Print CURL result.
echo $ch_result;
?>
显然,您需要更改 XML 以适应您想要的图层等。 如果从服务器的角度来看它不在本地主机上,您还需要将curl_init 中的URL 更改为您的服务器。
如果一切正常,应该打印很多奇怪的文本,因为它是返回的图像。有时您可以在顶部的文本中看到 PNG,因为它是 png 标题的一部分。
关于geoserver - 使用 XML (GeoServer) 的 PHP Post WMS GetMap 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978490/