Menu

[r837]: / branches / Release-5-2-2 / php-java-bridge / INSTALL.J2SE  Maximize  Restore  History

Download this file

130 lines (89 with data), 4.5 kB

Overview
--------

The PHP JSR 223 interface associates a managed and persistent PHP
executable or the current PHP web context with a JSR 223 ScriptEngine.

This document describes how to call PHP methods from Java using the
JSR 223 interface. For example it is possible to use PHP as a script
plugin for a Java report engine such as BIRT; for formula fields,
group selection formulas, etc. Please see the INSTALL.J2EE document
for details how to embed PHP into a pure J2EE application server or
how to call Java methods from PHP.

The PHP/Java Bridge can be installed with one copy operation:

* the "JavaBridge.jar" and the "php-script.jar" should be copied into
the J2SE java.ext.dirs directory.


Install PHP into J2SE
---------------------

* Install Java 1.6 or above. For older Java versions you'll need the
script-api.jar contained in the JavaBridge.war file.

* Install PHP 5.1.4 or above.

* Download and extract the php-java-bridge_j2ee*.zip archive into a
folder and type: java -classpath JavaBridge.war TestInstallation

* Copy the created "JavaBridge.jar" and "php-script.jar" from the
"ext" folder to the J2SE library path. On Windows the location depends
on the version and vendor of the JDK (see java.ext.dirs from the above
RESULT.html). On Linux the directory is:

  /usr/java/packages/lib/ext/


* Use the command "jrunscript" to evaluate PHP scripts from Java, for
example:

    jrunscript -l php phpFile.php 

  or:

    jrunscript -J-Dphp.java.bridge.php_exec=/usr/bin/php-cgi -l php phpFile.php


* A convenience ScriptEngine is available which makes it easier to
invoke PHP methods from Java. Example:

    System.setProperty("php.java.bridge.default_log_file", "");

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine e = manager.getEngineByName("php-invocable");

    e.eval("<?php class f {function a($p) {return java_values($p)+1;}}\n" +
                     "java_context()->setAttribute('f', 
                                  java_closure(new f()), 100); ?>");
    Invocable i = (Invocable)e;

    Object f = e.getContext().getAttribute("f", 100);
    System.out.println(i.invokeMethod(f, "a", new Object[] {new Integer(1)}));

    e.eval ((Reader)null); // release the continuation, flush output
    

* Another convenience ScriptEngine can be used to implement a
  REPL. Example:

  ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine e = manager.getEngineByName("php-interactive");
  System.out.println(e.eval("$a=122"));
  System.out.println(e.eval("$a=$a+1;"));
  System.out.println(e.eval("echo $a;"));


* Furthermore it is possible to run PHP scripts in servlets. The
  examples below use JSP to demonstrate the features (in JSP "this" is
  the Servlet, "application" is the ServletContext and "request",
  "response" the HttpServletRequest and HttpServletResponse objects.

  The web application needs a context listener declared in 
  WEB-INF/web.xml:

   <listener>
       <listener-class>php.java.servlet.ContextLoaderListener</listener-class>
   </listener>

  And the JavaBridge.jar, php-servlet.jar, php-script.jar, script-api.jar
  must be available in the WEB-INF/lib directory.

  Servlets can then access PHP scripts as follows:

  <%
    javax.script.ScriptEngine e = 
      php.java.script.EngineFactory.getPhpScriptEngine (this, 
                                                        application, 
                                                        request, response);
    e.getContext().setWriter (out);
    e.eval ("<?php phpinfo(); ?>");
    e.eval (new java.io.FileReader("otherScript.php"));
    ...
  %>

  The InvocablePhpScriptEngine can be accessed as follows:

  <%
    javax.script.ScriptEngine e = 
      php.java.script.EngineFactory.getInvocablePhpScriptEngine (this, 
                                                        application, 
                                                        request, response);
    e.getContext().setWriter (out);
    e.eval ("<?php function hello() {echo "hello";}; ?>");
    javax.script.Invocable i = (javax.script.Invocable) e;
    i.invokeFunction ("hello", new Object[]{});
    ...
    e.eval ((java.io.Reader)null);
  %>
   
  Note the terminating e.eval (...) above. It flushes the output
  generated by the previous invokeXXX function calls.

* Please see the JSR 223 specification for details.

* Please report bugs/problems to the mailing list:

    php-java-bridge-users@lists.sourceforge.net

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.