0

I'm making an admin panel in php to control my Java app. I know to how send a request in java to php. For example, I have this link:

myPage.com/admin/statusServers&id=10 (this page prints f.e "OK")

In Java, I can read this "OK", but how do I send "request" from php to java? Something like this:

myPage.com/admin/resetApp&pass=312&user=123

My intention is for the app to receive this, validate the user-pass combination, and then reset something. Do I need to make an infinite loop with 1-sec delay to check for requests, or something like this?

3
  • Can you explain where that java and where that php runs? On the same server? And why do you want a php frontend to a java application? When both things run on the same machine they have more ways to communicate. Commented Apr 23, 2016 at 20:14
  • php and java run on the same machine Commented Apr 23, 2016 at 20:31
  • Fix grammar, spelling, etc. Commented Apr 27, 2016 at 21:28

1 Answer 1

2

java and php are on the same machine ?

PHP TO JAVA

http://php-java-bridge.sourceforge.net/pjb/

So, I have a very basic test set up to see if i can send data from a php web page to a java app running on the same server.

The java app is dead simple, it just listens on a TCP socket for data

import java.io.*;
import java.net.*;

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
            connectionSocket.close();
         }
      }
}

Method 1

$host = "tcp://localhost"; 
$port = 6789;
$data = 'test' . PHP_EOL; 
$errstr = '';
$errno = '';

if ( ($fp = fsockopen($host, $port, $errno, $errstr, 3) ) === FALSE)
    echo "$errstr ($errno)";
else {
    print 'SUCCESS!<br />';
    fwrite($fp, $data);
    while (! feof($fp)) {
      echo fgets($fp, 4096);
    }
    fclose($fp);
}

Method 2

$host = "localhost"; 
$port = 6789;
$data = 'test';

if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error());
else 
{
    echo "Attempting to connect to '$host' on port '$port'...<br>";
    if ( ($result = socket_connect($socket, $host, $port)) === FALSE )
        echo "socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
    else {
        echo "Sending data...<br>";
        socket_write($socket, $data, strlen($data));
        echo "OK<br>";

        echo "Reading response:<br>";
        while ($out = socket_read($socket, 2048)) {
            echo $out;
        }
    }
    socket_close($socket);      
}

EDIT

JAVA TO PHP

There are many ways to do it I would think any system used to invoke an action command from php

if( isset($_GET["user"]) && isset($_GET["pass"]) ){
  echo system("java myjavaprogram . " .$_GET["user"])
  // Then args will be an array containing the strings $_GET["user"]
}

and the java program receive the command and fired action

public static void main(String [] args) {
   String user = args[0]; // user
   // etc.. etc 
}

You can also use curl parea make any request from php

if( isset($_GET["user"]) && isset($_GET["pass"]) ){

    $ch = curl_init("http://127.0.0.1:8080/servlet");
    $fp = fopen("test.txt", "w");

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}

I hope I've helped

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.