![]() |
| |||||||||
| Resin 3.1 Documentation Examples Changes Overview Installation Configuration Quercus SOA/IoC JSP Servlets and Filters Admin (JMX) EJB Amber Security Performance Hessian XML and XSLT Third-party Troubleshooting/FAQ Getting Started Overview Security Module Status Resin Module Java Interface List of PHP Applications |
Introduction to Quercus Running an Existing PHP Application on QuercusIn general, getting an application to run on Quercus instead of the standard PHP interpreter requires only a little extra work. To demonstrate this simple process, let's install WordPress. Suppose WordPress has been extracted to
Step 1. Create the
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<script-encoding>iso-8859-1</script-encoding>
</init>
</servlet-mapping>
</web-app>
Step 2. If you have not already done so, download the MySQL Connector/J JDBC driver into Step 3. The configuration for Resin/Quercus is now complete. Now you'll need to go through the application's installation routine. For Wordpress, go to http://localhost:8080/wordpress/wp-admin/setup-config.php to start the installation. That's it for Wordpress on Quercus. If you are encountering issues with a certain application, please see Resources for assistance. For a list of applications running successfully on Quercus, see List of PHP Applications Running on Quercus. Configuring QuercusPhp.iniAt the moment, Quercus does not have the option to read in an entire php.ini file upon startup. However, individial PHP initialization values can be set in web.xml. For example, to set the settings for sending mail:
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<php-ini>
<sendmail_from>my_email_address</sendmail_from>
<smtp_username>my_email_username</smtp_username>
<smtp_password>my_email_password</smtp_password>
</php-ini>
</init>
</servlet-mapping>
</web-app>
Character Encoding IssuesBecause Quercus is 100% Java, Quercus has native support for Unicode and will, by default, parse PHP scripts in UTF-8. This also means Quercus supports the new PHP 6 Unicode keywords like this Unicode typecast: To solution is to tell Quercus to parse PHP scripts using the correct character set (ISO-8859-1 for most applications). For example, to tell Quercus to use ISO-8859-1, add
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<script-encoding>iso-8859-1</script-encoding>
</init>
</servlet-mapping>
</web-app>
Garbage Output On BrowserThere are three encodings you need to worry about: script encoding, output encoding, and runtime encoding. By default, Quercus uses UTF-8 for all three. Script encoding is for when your scripts are in an encoding other than UTF-8. Output encoding is the charset used to display output to the browser. You can set it in your resin-web.xml:
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<php-ini>
<unicode.output_encoding>MY_ENCODING</unicode.output_encoding>
</php-ini>
</init>
</servlet-mapping>
</web-app>
There is another encoding that you need to know about. It is the unicode.runtime_encoding and it defaults to UTF-8. It is a PHP 6 directive that tells Quercus what encoding to assume a binary string is in when doing implicit conversions to Unicode. You would set runtime encoding in the same way as you would for output encoding. In PHP 6, there are two types of strings, Unicode and binary. A binary string is a string where the data is binary, the encoding is unknown, or the encoding is not Unicode (UTF-16). If you ever use a function that will likely return a binary string, then you probably need to set unicode.runtime_encoding. Quercus may convert your binary string to Unicode and then to your output encoding for output to the browser. If your runtime encoding is wrong, then you would see garbage in your browser. Compiling PHP Scripts for Increased PerformanceQuercus can automatically compile PHP scripts into Java classes for better performance. This is available only in Resin Professional. To turn on compilation, the
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<compile>true</compile>
</init>
</servlet-mapping>
</web-app>
Using DatabasesJDBC drivers are required to use databases in Quercus. There are JDBC drivers for MySQL, Oracle, SQLite, and many other database engines. The desired JDBC driver should be downloaded into Resin's For example, PHP mysql functions will only function properly when the MySQL Connector/J JDBC Driver is downloaded and placed in the If a database with JNDI name
<?php
// standard PHP
//mysql_connect($host, $username, $password, $dbname);
// using JNDI lookup
mysql_connect("jdbc/myDatabaseName");
?>
JNDI
Adding PHP Functions by Creating a Quercus ModuleThe core PHP functions are implemented inside Quercus modules. Quercus modules are the Java equivalent of PHP modules. All Quercus Modules need to implement AbstractQuercusModule. Functions defined in your modules are callable from within PHP script by using just the function name. Function names need to be distinct in order to prevent name collisions, though Quercus does support function overloading (for Java functions only). A typical Quercus Module looks like:
package example;
import com.caucho.quercus.env.Env;
import com.caucho.quercus.module.AbstractQuercusModule;
public class HelloModule extends AbstractQuercusModule
{
/**
* @param env provides Quercus environment resources.
* @param str
*/
public void HeLLo_TeST(Env env, String str)
{
// 'echos' the string
env.println("hello " + str);
}
}
<?php
// PHP 5 is case-insensitive
// just prints "hello me" to the browser.
hello_test("me");
?>
For a tutorial on how to implement your own Quercus module, see the Quercus Module tutorial. Using Plain Old Java Objects (POJO) in PHP ScriptsYou do not need to create a Quercus Module in order to use your Java code in a PHP script. Quercus provides the
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<class name="myPackage.MyClass"/>
</init>
</servlet-mapping>
</web-app>
<?php
// Quercus specific construct
import java.util.Date;
$date = java("Date", 123456789);
$list = java("java.util.ArrayList");
$class = java_class("java.lang.System");
$in = $class->in;
$time = $class->currentTimeInMillis();
?>
Quercus does automatic conversion from Java objects to PHP objects. Function overloading is supported (for Java functions only). For more information and a list of Java Annotations that you can use in your code, see Java Interface. Java Function Arguments/Return MarshalingQuercus does marshaling to and fro Quercus Values and Java objects. If a Java function requires a String, Quercus will automatically convert the internal Quercus StringValue to a String. If a Java function returns an For other Java Objects like For more information, see Java Interface. Quercus-specific FunctionsQuercus has several Quercus-only PHP functions for debugging and Java interfacing (JNDI, JMS, and JMX). For more information, see Resin PHP Functions. The list of debugging functions are below: Debugging Functions
Differences from PHP 6Quercus implements PHP 5 but supports most of PHP 6 language features that are available in the PHP 6 preview distribution. Differences from PHP 6 are:
Resources
| |||||||||