JavaPHP
JavaPHP是一个轻巧的Java库,允许将PHP代码执行到Java中,它不依赖任何依赖项,并且是完全独立的。
它可以与Web服务器(Nanohttpd,Java httpserver等…)或正常Java项目的一部分一起使用。
如何使用
JavaPHP</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>\”>
< repositories > < repository > < id >breadeatercdn</ id > < url >https://cdn.b**r*eadeater.fr/maven</ url > </ repository > </ repositories > < dependencies > < dependency > < groupId >fr.breadeater</ groupId > < artifactId > JavaPHP </ artifactId > < version >2.1.1</ version > </ dependency > </ dependencies >
- 如果不使用PHP FPM,请使用以下方式启动PHP FastCGI服务器
php-cgi -b 127.0.0.1:<port>
警告:出于安全原因,建议将PHP-CGI或PHP-FPM绑定到Localhost!
- 使用以下示例代码在Java中运行PHP文件:
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 .Options options = new JavaPHP .Options()
.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());
}
}\”>
public class JavaPHP Test { 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 . Options options = new JavaPHP . Options () . 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 ()); } }
贡献
您可以根据需要做出尽可能多的贡献,贡献者将在读书中列出。
注意:仅在Java代码中进行修改为贡献。
贡献者
(尚无贡献者)
执照
该项目已获得MIT许可证的许可,请参阅许可证以获取更多信息。
