Want to leverage existing PHP experience of your development team to accelerate implementation times? In this blog post I’m going to show you how to take advantage of new Mule PHP scripting module, that enables your applications to use good’old PHP as the programming language in Mule scripting components.

We’ve talked in the past on this blog about Mule being used as a backend for PHP, now PHP developers can code custom logic in Mule using PHP. Mule scripting component allows you to implement business processing logic on your Mule flows, as algorithms programmed on any programming language with an available scripting engine compliant with the JSR-223 spec. This spec, dictates a standard framework on which applications can rely to leverage processing tasks as program scripts to the scripting engine, independently of the engine’s programming language. JSR-223 establishes a common interface for Java apps to invoke the execution of scripts and to pass data in and out of the engine execution context. JSR-223 compliant scripting engines are simple Java libraries that implement this common interface, and performs the interpretation/compilation and execution of the programming language structures. Mule ships with JSR-223 compliant scripting engines for Groovy, Python, JavaScript and Ruby programming languages out-of-the-box.

The new Mule PHP scripting module leverages the open source project Quercus, which  provides a pure-java implementation of the PHP-5 programming language with the most commonly used modules. Since this implementation is JSR-223 compliant, we can employ Quercus to execute PHP scripts on Mule scripting components.

Let’s see how this works by creating an example project in MuleStudio:

To use the Mule PHP scripting module, you’ll just need to download the module JAR file (mule-module-.jar) from the MuleForge and add it to your Studio project, just dragging and dropping it to your Studio project:

Next, add the jar to your project build path, by right-clicking on it on the package explorer and selecting the Build Path -> Add to build path context menu option:

Now, let’s create a simple mule flow including a Scripting component. Add a HTTP inbound endpoint and a Script component by just dragging and dropping them from the palette into the canvas:

Next, double click on the Scripting component to open the properties window. Enter “php” on the Engine combobox (write it, the list only shows out-of-the-box engines), and a very simple PHP on the code text area:

The XML configuration of the script is shown in this gist:

Observe how you have access to the Mule context bindings on your scripts as standard PHP variables, such as $payload and $log. This simple script, just replaces the message payload with a greeting string, including the message payload received by the HTTP inbound endpoint (which is justs the URL path).

Now, start the application by right clicking on the flow file and hitting on the Run As -> Mule Application context menu option:

Point your favorite browser to “http://localhost:8081/phptest” or any other path, and observe the response:

 

Also, switch back to MuleStudio to observe the message sent from the PHP script to the console:

This is pretty much all you need to do in order to embed PHP to your Mule application’s scripting components. Want to see a more complex example? The phpscripting example application implements more elaborated business logic in PHP, taking advantage of the simple yet powerfull JSON manipulation capabilities of PHP:

The flow XML configuration goes as follows:

This sample application uses PHP to analyze credit requests coming in JSON format through the HTTP protocol, by POSTs to “http://localhost:8081/runPHP”. According to the first three characters of the customer ID, the requested amount for credit is verified against an upper limit based on the customer average income. If the customer ID starts with “ABC”, he is allowed to borrow amounts up to 3 times his average income. If the customer ID starts with “DEF”, he is allowed to borrow amounts up to 2 times his average income:

So, according to this business rules that we implemented using the PHP script, this would be an approved credit request:

And this one, will be rejected:

The answer is also built by a PHP script as follows:

Observe how we combine data from the message properties and the message payload to build the response message, by using the Mule context bindings such as $payload and $message, as we did on the first simple example.

Wrapping up, in this blog post you have seen an extremely easy way to extend Mule in order to use PHP as your favorite scripting language, allowing you to take advantage of PHP modules, existing experience of your development team on this programming language and even re-use existing algorithms written on PHP.

 

 

 

No related posts.

Interested in 3 days of knowledge sharing, hands-on labs, industry focused sessions, and plenty of networking? Register for the premier integration event, CONNECT London »

6 Responses to “Using PHP as the scripting language in Mule”

mrizkir August 5th, 2012, 12:37 am

i’m try to calll mysql_connect (“root”,”test”,”") function. I got error like this :

Component that caused exception is: ScriptComponent{blognotifierFlow1.commponent.5120670}. Message payload is of type: String.

My question is whether all the functions in php recognizable?

Jesus De Oliveira August 16th, 2012, 10:45 am

Hi Mrizklr,

Supported functions are listed on quercus documentation:

http://www.caucho.com/resin-3.1/doc/quercus.xtp#modulehighlights

In order to use MySQL you need to add the MySQL JDBC driver jar to your project (drop the jar on your project root in studio, right click on it and add it to the build path with the menu option).

Even so, I strongly recommend using the Mule JDBC transport to interact with your databases, in order to benefit from Mule managed connection pools, better performance and the advantage of decoupling data exchange from routing/business logic in appropriate components in your Mule application (endpoints for data input/output and scripting component to implement routing/business logic):

http://www.mulesoft.org/documentation/display/MULE3USER/JDBC+Transport+Reference
http://www.mulesoft.org/documentation/display/MULE3STUDIO/Database+(JDBC)+Endpoint+Reference

Welcome to Mule Enterprise | CloudHub August 21st, 2012, 11:47 pm

[...] processors allow you to do the same in Mule Expression Language, Groovy, Python, Ruby, JavaScript, PHP and indeed any language which implements the JSR-223 scripting spec for the JVM. These extension [...]

Extending Mule with DevKit – the LDAPConnector October 23rd, 2012, 2:23 am

[...] Scripting processors allow you to execute custom logic in Groovy, Python, Ruby, JavaScript, PHP and indeed any language which implements the JSR-223 scripting spec for the JVM; and of course [...]

Welcome to Mule Enterprise - M-Square December 2nd, 2012, 1:07 pm

[...] processors allow you to do the same in Mule Expression Language, Groovy, Python, Ruby, JavaScript, PHP and indeed any language which implements the JSR-223 scripting spec for the JVM. These extension [...]

Extending Mule with DevKit – the LDAPConnector - M-Square December 2nd, 2012, 1:15 pm

[...] our Scripting processors allow you to execute custom logic in Groovy, Python, Ruby, JavaScript, PHP and indeed any language which implements the JSR-223 scripting spec for the JVM; and of course Java [...]

Leave a Comment