Search code examples
javaphpjava-mehttpconnection

How to post a named variable to a PHP file?


I want to post among other data a String variable to a PHP file by using the HttpConnection stuff. The first data is the byte[] data returned from a recordstore. So it should be posted alone. So how to post the String variable also ?


Solution

  • You can pass the data to a PHP file using GET or POST methods.

    Get method is the easy way to pass simple data. Using GET you can add the variable to the URL

    Example:

    192.168.1.123/myproject/uploads/treatphoto.php?myVariable1=MyContent&myVariable2=MyContent2
    

    And in PHP:

    $content1 = $_GET['myVariable1'];
    $content2 = $_GET['myVariable2'];
    

    Also the content of "MyContent" needs to be an string encoded. using any UrlEncoder.

    To pass a byte[] array using this method you need to convert the byte array to an string encoded in some printable encoding like base64

    The GET method also has a sort limit of data that can be passed safely (usually 2048 bytes)

    The other method "POST" is more complex (but not a lot), way to add more data.

    You need to prepare the HttpConnection to pass the data as POST. Also the data stored in urlParamenters need to be according to the url enconding. Passing the data using post is similar to GET but instead of adding all the variables next to the url the varuiables are added in the Stream of the httpConnection request.

    example of the java code:

    String urlParameters = "myVariable1=myValue1&myVariable2=myValue2";
    
    HttpURLConnection connection = null;  
    try {
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
    
      // Use post and add the type of post data as URLENCODED
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    
      // Optinally add the language and the data content
      connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  
    
      // Set the mode as output and disable cache.
      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);
    
      //Send request
      DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();
    
    
      // Get Response    
      // Optionally you can get the response of php call.
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      return response.toString();
    

    The php is similar, you only need to replace $_GET by $_POST:

    $content1 = $_POST['myVariable1'];
    $content2 = $_POST['myVariable2'];