PDA

View Full Version : Using PHP code from Java


jooray
06-16-2010, 08:21 AM
Hello,

I would like to use some custom PHP code from Java. This means no servlet, I would like to use Caucho simply as PHP interpreter (like JRuby for Ruby). Is this possible?

When reading documentation, I have read how to use Java classes from PHP. Is it possible to do it the other way around (creating Quercus instance, telling it which script to load and return a PHP object instance, that I can use from Java)?

Juraj.

emil
06-17-2010, 06:16 PM
Hi Juraj,

You can use the javax.script interface:


import javax.script.*;
import java.io.*;

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("quercus");

StringReader reader = new StringReader("<? return 1 + 1; ?>");

out.println("1 + 1: " + engine.eval(reader));


Replace the StringReader with a FileReader for files, etc.

Emil

peter_jones_jr
06-18-2010, 08:50 AM
In PHP you must use exit() to return a value from PHP, look here http://php.net/manual/en/function.return.php. A return 1+1; will exit the script successfully. If quercus behaves differently, we've found a bug. :)

<blockquote>
<code>
import java.io.*;
import javax.script.*;
import java.util.*;

public class test {
public static void main(String s[]) throws Exception {

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName('php');
StringReader reader = new StringReader('<?php exit (1 + 1); ?>');

System.out.println("1 + 1: " + engine.eval(reader));
}
}
</code>
</blockquote>

achinaseller
08-16-2010, 02:39 AM
hi,friends,i am a young man who want to create myself bussiness in France,i want to wholesale some high quality,low prices products to sell,and i know China goods are very cheap.so i search china electronics wholesale (http://www.achinaseller.com/) seller in google,i have looked many wholesale website,but i found a web ,it have Charming picture and Attractive prices,i want to select it,its web address is china wholesale (http://www.achinaseller.com) web. and the casio original watches such as g shock (http://www.achinaseller.com/jewelry.asp?/Casio-G-SHOCK-G-315RL-2AV.htm)watches, it have the Casio watches offical authorized proof,I want wholesale,please give me suggestion,thank you very muchbest regardsjack