DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI Is Making PHP Cool Again
  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide

Trending

  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Mule 3: Execute PHP Script in Mule ESB

Mule 3: Execute PHP Script in Mule ESB

In this article, I will explain how to execute a PHP Script within the Mule Flow. Also, take a look at the code.

By 
Enrico Rafols Dela Cruz
·
Sep. 05, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this article, I will explain how to execute a PHP Script within the Mule Flow. First, we need to include the mule-module-php.jar file on our project. Mule needs this library to be able to execute the PHP script at runtime. You can download the jar file here: https://github.com/jdeoliveira/mule-module-php/downloads.

Image title

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">


<flow name="forumFlow">
        <poll doc:name="Poll">
            <fixed-frequency-scheduler frequency="5000"/>
            <logger message="Start Of Process" level="INFO" doc:name="Logger"/>
        </poll>
        <set-payload value="Mulesoft" doc:name="Set Mulesoft As Payload"/>

<scripting:component doc:name="Script">
  <scripting:script engine="php">
       <scripting:text><![CDATA[<?php
            require "src/main/resources/User.php";

            $h = new User("Enrico",15);
            $log->info("Hello, " . $h->getName(). "! You are ". $h->isAdult());
            $h = new User("Jose",39);
            $log->info("Hello, " . $h->getName(). "! You are ". $h->isAdult());

            return "Hi From PHP To " . $payload
?>]]></scripting:text>
  </scripting:script>
</scripting:component>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

The above Mule flow runs every 5 seconds to execute the Script Component that contains our PHP code. Inside that component, it's simply initialize the PHP class User located in /src/main/resources/ and invoke its methods or functions. Also, note that the Script Component engine should be set to php.

<scripting:script engine="php">

/src/main/resources/User.php

<?php
  class User {

    private $name;
    private $age;

    function __construct( $name, $age ) {
      $this->name = $name;
      $this->age = $age;
    }

    function getName() {
      return $this->name;
    }

    function isAdult() {
      return $this->age >= 18?"an Adult":"Not an Adult";
    }
  } 
?>

TESTING:

Image title

Thanks for reading, and let me know your thoughts in the comments section. 

PHP Enterprise service bus

Opinions expressed by DZone contributors are their own.

Related

  • AI Is Making PHP Cool Again
  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook