Skip to content

Commit b8b10e7

Browse files
committed
Entirely reworked JavaPHP to use PHP FastCGI via TCP/IP
1 parent 8511996 commit b8b10e7

File tree

14 files changed

+470
-297
lines changed

14 files changed

+470
-297
lines changed

.idea/discord.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 BreadEater
3+
Copyright (c) 2025 BreadEater
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,74 @@
11
## JavaPHP
22

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.
64

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.
156

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+
-
1612
### 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:
2315

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+
```
3019

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 !
3221

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:
3623

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());
3968
}
4069
}
4170
```
4271

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-
4772
### Contribution
4873
You can contribute as much as you want, contributors will be listed in the README.
4974

pom.xml

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,12 @@
66

77
<groupId>fr.breadeater</groupId>
88
<artifactId>JavaPHP</artifactId>
9-
<version>1.0-ALPHA</version>
9+
<version>2.0-RELEASE</version>
1010

1111
<properties>
12-
<maven.compiler.source>21</maven.compiler.source>
13-
<maven.compiler.target>21</maven.compiler.target>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
1616

17-
<build>
18-
<plugins>
19-
<plugin>
20-
<artifactId>maven-assembly-plugin</artifactId>
21-
<configuration>
22-
<archive>
23-
<manifest>
24-
<mainClass>fr.breadeater.Main</mainClass>
25-
</manifest>
26-
</archive>
27-
<descriptorRefs>
28-
<descriptorRef>jar-with-dependencies</descriptorRef>
29-
</descriptorRefs>
30-
</configuration>
31-
<executions>
32-
<execution>
33-
<id>make-assembly</id>
34-
<phase>package</phase>
35-
<goals>
36-
<goal>single</goal>
37-
</goals>
38-
</execution>
39-
</executions>
40-
</plugin>
41-
</plugins>
42-
</build>
43-
4417
</project>

src/main/java/fr/breadeater/JavaPHP.java

Lines changed: 0 additions & 205 deletions
This file was deleted.

0 commit comments

Comments
 (0)