JavaPHP is a lightweight Java library that permits to execute PHP code into Java, it does not rely on any dependencies and is fully standalone.
It can be used with Webservers (NanoHTTPD, Java HttpServer, etc...) or as a part of a normal Java project.
- Execute PHP files from Java
- Supports all HTTP methods and headers
- Custom error handling via
Consumer<Exception>
- Compatible with PHP-FPM or
php-cgi
command
- Import JavaPHP to your project.
- If not using PHP FPM, start a PHP FastCGI server using:
php-cgi -b 127.0.0.1:<port>
Warning: binding PHP-CGI or PHP-FPM to localhost is recommended for security reasons !
- Run your PHP file in Java using this following example code:
public class JavaPHPTest {
public static void main(String[] args){
JavaPHP javaphp = new JavaPHP(new InetSocketAddress("127.0.0.1", 7000)); // Create a JavaPHP instance with your PHP-CGI / PHP-FPM server address as parameter
Request request = new Request(); // Create a request instance to specify method, body, etc...
Headers headers = new Headers(); // Create a new Headers instance (if not already created), will be used to specify HTTP headers
headers.add("Content-Type", "text/plain"); // Sets Content-Type to text/plain
request.setRequestMethod("POST"); // Sets request method to POST
request.setRequestPath("/");
request.setRequestBody("Hello World !"); // Sets 'Hello World !' as body
request.setRequestHttpVersion("HTTP/1.1");
request.setRequestAddress(new InetSocketAddress("127.0.0.1", 47829)); // The remote address (basically the user address)
request.setRequestHeaders(headers); // Gives the headers to the request instance
request.setIsHTTPS(false); // Sets HTTPS to false for example
// Specifies the options that will be used when running a PHP file
// Note: The location of the PHP file is the same location as the PHP-CGI / PHP-FPM working directory, same for Document Root
JavaPHP.RunOptions options = new JavaPHP.RunOptions()
.setPHPDocumentRoot(new File("./").getAbsolutePath())
.setPHPFilepath(new File("./index.php").getAbsolutePath())
.setPHPServerSoftwareName("Java") // Sets the Server Software name (e.g Apache, Nginx, etc...)
.setPHPServerAddress("127.0.0.1") // The address where the HTTP server listen to
.setPHPServerPort(80); // The port where the HTTP server listen to
// Handles error if a error occurs while running the PHP file
// In this case we just simply throw the error
javaphp.onError((err) -> {
try {
throw err;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
// Runs the PHP file and get the Response containing headers, body and status code given by PHP FastCGI
Response response = javaphp.run(options, request);
// Prints the results in the terminal
response.getResultHeaders().forEach((name, value) -> System.out.println(name + ": " + value.getFirst()));
System.out.println(response.getResultStatusCode());
System.out.println(response.getResultBody());
}
}
You can contribute as much as you want, contributors will be listed in the README.
NOTE: Only modifications in the Java code counts as contribution.
(No contributors yet)
This project is licensed under the MIT license, see LICENSE for more infos.