I’m currently working on a BI project where scripts are written in Python and orchestrated by Apache Airflow. Airflow was originally designed and built for the Python ecosystem. But as a PHP developer, I want to use Airflow to orchestrate PHP stuff.
Airflow tasks (called DAG for Directed Acyclic Graph) are executed through operators. One of them is the BashOperator which allows to run a command directly on an Airflow worker. So if PHP is installed on it, a script can be executed.
Let’s take this simple PHP script as an example:
<?php
echo "Hello from PHP script!\n";
echo "Execution time: " . date('Y-m-d H:i:s') . "\n";To create a DAG, we need to write some Pyhton code (it’s the only moment where Python is needed):
from airflow import DAG
from airflow.providers.standard.operators.bash import BashOperator
from datetime import datetime
with DAG(
dag_id="run_php_script",
description="A DAG that runs a PHP script",
schedule=None,
start_date=datetime(2024, 1, 1),
catchup=False,
tags=["php"],
) as dag:
run_php_script = BashOperator(
task_id="run_php_script",
bash_command="php /path/to/scripts/hello.php",
)The previous code triggers a PHP script into Airflow. Standard output and error are automatically captured in logs.
If you want to try it yourself, this repository has everything you need to spin up an Airflow environment ready to go.