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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Trending

  • Building Resilient Go Apps: Mocking and Testing Database Error Responses
  • Why Tailwind CSS Can Be Used Instead of Bootstrap CSS
  • Indexed Views in SQL Server: A Production DBA's Complete Guide
  • The Cybersecurity Blind Spot in DevOps Pipelines
  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.4K 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

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: