最近在工作的过程中,有一个需求是在外部程序中对GeoServer进行管理操作,通过查阅资料发现GeoServer的REST接口可以满足需求。REST接口使用HTTP调用的方式,无需登录Web管理界面就可以对GeoServer进行简单的调用和配置。
GeoServer官网对REST的配置API和示例的介绍地址为:http://docs.geoserver.org/stable/en/user/rest/index.html
下面的程序通过使用REST接口,请求GeoServer中图层的列表。
<?<span php </span><span $url</span> = "http://localhost:8180/geoserver/rest/layers/"<span ; </span><span $ch</span> = curl_init(<span $url</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, <span true</span>);<span //</span><span 设置为true,表示获取的内容以字符串的形式返回</span> curl_setopt(<span $ch</span>, CURLOPT_VERBOSE, <span true</span>);<span //</span><span 设置为true,返回执行过程中的异常</span> curl_setopt(<span $ch</span>, CURLOPT_GET, <span True</span><span ); </span><span $passwordStr</span> = "admin:geoserver";<span //</span><span geoserver的用户名:密码</span> curl_setopt(<span $ch</span>, CURLOPT_USERPWD, <span $passwordStr</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HTTPHEADER, <span array</span>("Accept:application/json"));<span //</span><span HTTP请求头信息</span> <span $successCode</span> = 200<span ; </span><span $buffer</span> = curl_exec(<span $ch</span><span ); </span><span $info</span> = curl_getinfo(<span $ch</span><span ); curl_close(</span><span $ch</span><span ); </span><span if</span>(<span $info</span>['http_code'] != <span $successCode</span><span ){ </span><span $msgStr</span> = "请求失败!"<span ; </span><span echo</span> <span $msgStr</span><span ; }</span><span else</span><span { </span><span $outputArray</span> = json_decode(<span $buffer</span>, <span true</span><span ); </span><span $layerInfos</span> = <span $outputArray</span>['layers']['layer'<span ]; </span><span foreach</span>(<span $layerInfos</span> <span as</span> <span $k</span> => <span $v</span><span ){ </span><span $layerNames</span>[] = <span $v</span>['name'<span ]; } </span><span echo</span> json_encode(<span $layerNames</span><span ); } </span>?>
博客声明:
本博客中的所有文章,除标题中注明“转载”字样外,其余所有文章均为本人原创或在查阅资料后总结完成,引用非转载文章时请注明此声明。—— 博客园-pallee