0

I'm beginning in PHP pages.. I'm trying to build a page that allows a user to create a table in Geoserver/Postgresql, if he/she provides user/password. I've made a html form with those 3 infos, but I get the error 500 message, which I don't know how to debug. This is why I need your help, because I'm a beginner with PHP, CURL.

<!DOCTYPE  html>
<html>
    <body>
        <h2>PG GS input test</h2>
        <form  method="POST"  action="test1.php">
            User:<br>
            <input  type="text"  name="user">
            <br>
            Password:<br>
            <input  type="password"  name="password">
              <br>
            New tablename:<br>
            <input  type="text"  name="table">
            <br><br>
            <input  type="submit"  name="submit">
        </form>
    </body>
</html>

and the test1.php:

<?php
    // Open log file
    $logfh = fopen("GeoserverPHP.log", 'w') or die("can't open log file");

    // Initiate cURL session
    $service = "https://mappingforyou.eu/geoserver/"; // replace with your URL
    $ws = "workspace";
    $ds = "datastore";
    $request = "workspaces/" . $ws . "/datastores/" . $ds . "/features";
    $url = $service . $request;
    $ch = curl_init($url);

if (isset($_POST['submit'])) {
    $user = $_POST['user'];
    $password = $_POST['password'];
    $table = $_POST['table'];
    
    // Optional settings for debugging
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //option to return string
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, $logfh); // logs curl messages

    //Required POST request settings
    curl_setopt($ch, CURLOPT_POST, True);
    //$passwordStr = "admin:geoserver"; // replace with your username:password
    curl_setopt($ch, CURLOPT_USERPWD, GEOSERVER_USER);

    //POST data
    curl_setopt($ch, CURLOPT_HTTPHEADER,
              array("Content-type: text/xml"));
    $xmlStr = "<featureType>";
    $xmlStr = "<name>".$table."</name>";
    $xmlStr = "<nativeName>".$table."</nativeName>";
    $xmlStr = "<title>".$table."</title>";
    $xmlStr = "<srs>EPSG:4326</srs>";
    $xmlStr = "<attributes>";
    $xmlStr = "<attribute>";
    $xmlStr = "<name>geom</name>";
    $xmlStr = "<binding>com.vividsolutions.jts.geom.".$geometry."</binding>";
    $xmlStr = "</attribute>";
    $xmlStr = "<attribute>";
    $xmlStr = "<name>description</name>";
    $xmlStr = "<binding>java.lang.String</binding>";
    $xmlStr = "</attribute>";
    $xmlStr = "<attribute>";
    $xmlStr = "<name>timestamp</name>";
    $xmlStr = "<binding>java.util.Date</binding>";
    $xmlStr = "</attribute>";
    $xmlStr = "</attributes>";
    $xmlStr = "</featureType>";
    
   
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlStr);

    //POST return code
    $successCode = 201;

    $buffer = curl_exec($ch); // Execute the curl request

    // Check for errors and process results
    $info = curl_getinfo($ch);
    if ($info['http_code'] != $successCode) {
      $msgStr = "# Unsuccessful cURL request to ";
      $msgStr .= $url." [". $info['http_code']. "]\n";
      fwrite($logfh, $msgStr);
    } else {
      $msgStr = "# Successful cURL request to ".$url."\n";
      fwrite($logfh, $msgStr);
    }
    fwrite($logfh, $buffer."\n");

    curl_close($ch); // free resources if curl handle will not be reused
    fclose($logfh);  // close logfile

?>

Although CURL gives me a positive message:

Curl: Enabled

with

<?php
echo 'Curl: ', function_exists('curl_version') ? 'Enabled' . "\xA" : 'Disabled' . "\xA";
?>
user3840170
  • 26,597
  • 4
  • 30
  • 62
Vincent Dc
  • 117
  • 5
  • What do your server logs say about the error? – Phil Jun 16 '22 at 23:28
  • You keep overwriting the `$xmlStr` variable. Did you mean to append to it instead with `.=`? – Phil Jun 16 '22 at 23:29
  • @Phil yes I meant . = ... i will correct it.. i'm trying to copy things from http://cnokello.blogspot.com/2012/09/geoserver-rest-api-with-php.html?m=1 and understand the code – Vincent Dc Jun 16 '22 at 23:45
  • @Phil I don't know how to access server logs... I have apache httpd on almalinux and geoserver on tomcat or an embedded jetty.. i will look at this tomorrow – Vincent Dc Jun 16 '22 at 23:46

0 Answers0