Overview
--------

This document describes how to call PHP methods from Java. 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" must 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 binary download into a
folder and double-click on test.sh or test.bat. Check the RESULT.html.

* 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 -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);


* 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;"));
  e.eval((Reader)null);


* Furthermore it is possible to execute remote scripts running in a
  FastCGI or Apache/IIS server:

  ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine e = manager.getEngineByName("php-invocable");
  Reader r = new php.java.script.URLReader(new URL("http://localhost:80/beanMethods.php"));
  e.eval(r);
  Invocable i = (Invocable)e;
  ...
  e.eval((Reader)null);


* Please see the JSR 223 specification for details.

* Please report bugs/problems to the mailing list:

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

