Solved Post data to php script?

Discussion in 'Plugin Development' started by Metal Julien, Jul 17, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Metal Julien

    Hi, I'm trying to give my php script data, the php is waiting for it with a GET function. How do I do this with java?

    PHP:
    Code:
    <?php
     
     
    $filename = 'servers.txt';
     
    $file = file_get_contents($filename);
     
     
     
    if(!strpos($file, $_GET['ip'])) {
     
    $daten = $_GET['ip'];
    $dateihandle = fopen($filename,"a");
    fwrite($dateihandle, "\r\n".$daten);
    fclose($dateihandle);
     
     
    }
     
     
     
     
     
     
    ?>
    The simpliest way would be to call the link xxxxxxx/servers.php?ip=123.123.123.123

    But how do I call a link so that the GET post works?
     
  2. Offline

    caseif

    I don't quite understand what you're asking. If you want to send data to a PHP script, you've got it down. You just need to send the post request from Java to a URL containing the GET data.
     
    Metal Julien likes this.
  3. Offline

    Zethariel

    Metal Julien
    This is a more PHP than Java related question, but still:

    PHP generates a page when a user requests it. If you want the data to persist, it's best to use a MySQL database. Otherwise, unless you force MC to open a browser with the specified page, the query will just fizzle out into dead space.

    You can also try and write a file to your PHP hosting service and save in that file, but I believe that sending data to MySQL is simpler.
     
    Metal Julien likes this.
  4. Offline

    blablubbabc

    You could take a look at how metrics does it. It does it similiar to this:

    // collect data:
    final StringBuilder data = new StringBuilder();
    data.append(encode("mydata")).append('=').append(encode("some data"));
    // more data:
    /*
    private static void encodeDataPair(final StringBuilder buffer, final String key, final String value) throws UnsupportedEncodingException {
    buffer.append('&').append(encode(key)).append('=').append(encode(value));
    }
    // encode(..) encodes the text to utf-8, see below for how that works
    */
    encodeDataPair(data, "name", plugin.getServer().getServerName());
    // Create the url: http://blablah.de/yourscript.php
    URL url = new URL(URL);

    // Connect to the website
    URLConnection connection;
    connection = url.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setConnectTimeout(5000);
    connection.setReadTimeout(5000);

    // Write the data
    final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    // data is a string to send, encoded in UTF-8 (URLEncoder.encode(text, "UTF-8");)
    writer.write(data.toString());
    writer.flush();

    // Now read the response
    final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    final String response = reader.readLine();

    // close resources
    writer.close();
    reader.close();

    if (response == null || response.startsWith("ERR")) {
    throw new IOException(response);
    } else {
    //handle response
    }
     
    Metal Julien likes this.
  5. Offline

    Metal Julien

    blablubbabc I tried exactly that!
    Code:
    try {
                URL url = new URL("blabla/servers.php");
                  URLConnection connection = url.openConnection();
                  connection.setDoOutput(true);
     
                  connection.setConnectTimeout(5000);
               
                    OutputStreamWriter out = new OutputStreamWriter(
                                                    connection.getOutputStream());
                    out.write("ip=" + "heyyyyyy");
                    out.flush();
                    out.close();
     
                //  InputStream is = url.openConnection().getInputStream();
                // BufferedReader reader = new BufferedReader( new InputStreamReader( is )  );
     
               
               
            } catch (MalformedURLException e) {
    Doesn't work..

    blablubbabc It adds "" to the list :S What am I doing wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    blablubbabc

    In your php script you can get the data via:

    $_POST['ip']

    Edit: which list?
     
    Metal Julien likes this.
  7. Offline

    Metal Julien

    Got it! I have to use $_POST, not $_GET. Thanks to all!

    blablubbabc That's it, thank you!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page