[GIS] GeoServer create a datastore using PHP and the REST API

geoserver-rest-apiPHP

First of all I hope you forgive my bad English,I'm trying to create a datastore in GeoServer using PHP so can you please give me an example that can guide me to achieve it.

The code I have is:

$app->match('/send', function() use ($app){
$curl = new Curl();
$encoder =  [new XmlEncoder('datastore'), new \Symfony\Component\Serializer\Encoder\JsonEncoder()];
$normalizer = [new \Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer()];
$serializer = new \Symfony\Component\Serializer\Serializer($normalizer, $encoder);
$service = "http://localhost:8080/geoserver/"; // replace with your URL
$reqt = "rest/workspaces.xml"; // to add a new workspace
$url = $service . $reqt;
$curl->setOpt(CURLOPT_POST, True);
$curl->setBasicAuthentication('admin', 'geoserver');
$json = json_encode($curl->get($url));
$data = json_decode($json, TRUE);
$request = Request::createFromGlobals();

if($request->isMethod('POST')){
    $params  = [
            'name' => $request->get('name'),
            'type' => 'GeoTIFF',
            'description' => $request->get('description'),
            'url' => __DIR__ . '/' .$file->getClientOriginalName(),
            'enabled' => 'on' == $request->get('enabled') ? 'true' : 'false',

    ];
    $curl->setOpt(CURLOPT_UPLOAD, TRUE);
    $json = $app['serializer']->serialize($data, 'json');      
    $curl->setHeader("Content-type"," application/json");
    $target = "http://localhost:8080/geoserver/rest/workspaces/{$request->get('workspace')}/datastores";
    return new Response($curl->post($target, $json));
}

return $app['twig']->render('form.html.twig', ['data' => array_shift($data)]);
})->bind('send');

Best Answer

There are some PHP REST samples already mentioned at this other answer but links are dead.

You can also use the samples at IBM DeveloperWorks "Get started with GeoServer and its REST API" (to download and in the post).

Related Question