|
1 | 1 | ## JavaPHP |
2 | 2 |
|
3 | | -JavaPHP is a lightweight Java library that permits to execute PHP code into Java so instead of using Java servlets, |
4 | | -you can just use PHP and execute it as a part of your existing Java code, does not rely on any dependencies, its fully standalone, |
5 | | -and you can use it in a compiled .jar file or in a simple .java file without having to compile it. |
| 3 | +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. |
6 | 4 |
|
7 | | -It can be used with Webservers (NanoHTTPD, Java socket, etc...) or as a part of your Java app. |
8 | | - |
9 | | -### Features |
10 | | -- PHP file / code execution in java |
11 | | -- Customizable error handling |
12 | | - |
13 | | -### Planned features |
14 | | -- PHP file / code execution over TCP/IP using PHP-CGI |
| 5 | +It can be used with Webservers (NanoHTTPD, Java HttpServer, etc...) or as a part of a normal Java project. |
15 | 6 |
|
| 7 | +- Execute PHP files from Java |
| 8 | +- Supports all HTTP methods and headers |
| 9 | +- Custom error handling via `Consumer<Exception>` |
| 10 | +- Compatible with PHP-FPM or `php-cgi` command |
| 11 | +- |
16 | 12 | ### How to use |
17 | | -Simply import it to your file and do like this: |
18 | | - |
19 | | -```java |
20 | | -import java.io.File; |
21 | | -import java.util.HashMap; |
22 | | -import java.util.Map; |
| 13 | +- Import JavaPHP to your project. |
| 14 | +- If not using PHP FPM, start a PHP FastCGI server using: |
23 | 15 |
|
24 | | -public class Main { |
25 | | - public static void main(String[] args) throws Throwable { |
26 | | - JavaPHP javaphp = new JavaPHP(false); |
27 | | - Map<String, String> env = new HashMap<>(); |
28 | | - |
29 | | - env.put("REQUEST_METHOD", "GET"); |
| 16 | +```text |
| 17 | +php-cgi -b 127.0.0.1:<port> |
| 18 | +``` |
30 | 19 |
|
31 | | - javaphp.setErrorCallback((error) -> System.out.println("ERROR: " + error)); |
| 20 | +<strong>Warning:</strong> binding PHP-CGI or PHP-FPM to localhost is recommended for security reasons ! |
32 | 21 |
|
33 | | - javaphp.setPHPVars(env); |
34 | | - javaphp.run(new File("./test.php").getAbsolutePath()); |
35 | | - javaphp.runWithCli("echo 'Hello World !';"); |
| 22 | +- Run your PHP file in Java using this following example code: |
36 | 23 |
|
37 | | - System.out.println(javaphp.getResult()); |
38 | | - System.out.println(javaphp.getInlineResult()); |
| 24 | +```java |
| 25 | +public class JavaPHPTest { |
| 26 | + public static void main(String[] args){ |
| 27 | + 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 |
| 28 | + Request request = new Request(); // Create a request instance to specify method, body, etc... |
| 29 | + Headers headers = new Headers(); // Create a new Headers instance (if not already created), will be used to specify HTTP headers |
| 30 | + |
| 31 | + headers.add("Content-Type", "text/plain"); // Sets Content-Type to text/plain |
| 32 | + |
| 33 | + request.setRequestMethod("POST"); // Sets request method to POST |
| 34 | + request.setRequestPath("/"); |
| 35 | + request.setRequestBody("Hello World !"); // Sets 'Hello World !' as body |
| 36 | + request.setRequestHttpVersion("HTTP/1.1"); |
| 37 | + request.setRequestAddress(new InetSocketAddress("127.0.0.1", 47829)); // The remote address (basically the user address) |
| 38 | + request.setRequestHeaders(headers); // Gives the headers to the request instance |
| 39 | + request.setIsHTTPS(false); // Sets HTTPS to false for example |
| 40 | + |
| 41 | + // Specifies the options that will be used when running a PHP file |
| 42 | + // Note: The location of the PHP file is the same location as the PHP-CGI / PHP-FPM working directory, same for Document Root |
| 43 | + JavaPHP.RunOptions options = new JavaPHP.RunOptions() |
| 44 | + .setPHPDocumentRoot(new File("./").getAbsolutePath()) |
| 45 | + .setPHPFilepath(new File("./index.php").getAbsolutePath()) |
| 46 | + .setPHPServerSoftwareName("Java") // Sets the Server Software name (e.g Apache, Nginx, etc...) |
| 47 | + .setPHPServerAddress("127.0.0.1") // The address where the HTTP server listen to |
| 48 | + .setPHPServerPort(80); // The port where the HTTP server listen to |
| 49 | + |
| 50 | + // Handles error if a error occurs while running the PHP file |
| 51 | + // In this case we just simply throw the error |
| 52 | + javaphp.onError((err) -> { |
| 53 | + try { |
| 54 | + throw err; |
| 55 | + } catch (Exception e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // Runs the PHP file and get the Response containing headers, body and status code given by PHP FastCGI |
| 61 | + Response response = javaphp.run(options, request); |
| 62 | + |
| 63 | + // Prints the results in the terminal |
| 64 | + response.getResultHeaders().forEach((name, value) -> System.out.println(name + ": " + value.getFirst())); |
| 65 | + |
| 66 | + System.out.println(response.getResultStatusCode()); |
| 67 | + System.out.println(response.getResultBody()); |
39 | 68 | } |
40 | 69 | } |
41 | 70 | ``` |
42 | 71 |
|
43 | | -### Installation |
44 | | -Simply download the .jar / .java file from the repository Releases and import it in your project.<br> |
45 | | -For Maven or Gradle, add it to your local repository. |
46 | | - |
47 | 72 | ### Contribution |
48 | 73 | You can contribute as much as you want, contributors will be listed in the README. |
49 | 74 |
|
|
0 commit comments