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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 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
  • Inheritance in PHP: A Simple Guide With Examples

Trending

  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • The Hidden Latency of Autoscaling
  • The Network Attach Problem Nobody Warns You About
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  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 user avatar
Enrico Rafols Dela Cruz
·
Sep. 05, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.5K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • 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
  • Inheritance in PHP: A Simple Guide With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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