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.
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-php.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 script 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.