Home  >  Article  >  Backend Development  >  Using REST interface to get the layer list in GeoServer_PHP tutorial

Using REST interface to get the layer list in GeoServer_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:13:541390browse

Recently, in the process of work, there is a need to manage GeoServer in an external program. After consulting the data, I found that the REST interface of GeoServer can meet the demand. The REST interface uses HTTP calls to make simple calls and configurations to GeoServer without logging into the web management interface.

The introduction address of the REST configuration API and examples on the GeoServer official website is: http://docs.geoserver.org/stable/en/user/rest/index.html

The following program requests the list of layers in GeoServer by using the REST interface.

<?<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>?>

Blog Statement:

Except for the word "reprinted" in the title, all articles in this blog are original or summarized after consulting the information. Please indicate this statement when quoting non-reprinted articles. —— Blog Garden-pallee

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440337.htmlTechArticleRecently during the work process, there is a need to manage GeoServer in an external program. By consulting the information It was found that the REST interface of GeoServer can meet the needs. REST interface uses...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn