PHP code example of intelfric / n8n-php-automation
1. Go to this page and download the library: Download intelfric/n8n-php-automation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use DrMsigwa\Automation\Engine\FlowRunner;
use DrMsigwa\Automation\Models\Flow;
$flow = Flow::find(1);
$runner = new FlowRunner($flow);
$result = $runner->run(['key' => 'value']);
use DrMsigwa\Automation\Models\Flow;
use DrMsigwa\Automation\Engine\FlowRunner;
protected function schedule(Schedule $schedule)
{
// Run a specific flow every minute
$schedule->call(function () {
$flow = Flow::find(1);
(new FlowRunner($flow))->run();
})->everyMinute();
// Run daily at midnight
$schedule->call(function () {
$flow = Flow::find(2);
(new FlowRunner($flow))->run();
})->daily();
// Run every hour
$schedule->call(function () {
$flow = Flow::find(3);
(new FlowRunner($flow))->run();
})->hourly();
}
namespace App\Jobs;
use DrMsigwa\Automation\Models\Flow;
use DrMsigwa\Automation\Engine\FlowRunner;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
class ExecuteFlowJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
public function __construct(public int $flowId, public array $data = [])
{
}
public function handle()
{
$flow = Flow::find($this->flowId);
if ($flow) {
(new FlowRunner($flow))->run($this->data);
}
}
}
protected function schedule(Schedule $schedule)
{
$schedule->job(new ExecuteFlowJob(1))->everyMinute();
}
// In your routes/api.php or controller
Route::post('/webhook/trigger-flow/{flowId}', function ($flowId) {
$flow = Flow::find($flowId);
if (!$flow) {
return response()->json(['error' => 'Flow not found'], 404);
}
$data = request()->all();
$runner = new FlowRunner($flow);
$result = $runner->run($data);
return response()->json(['success' => true, 'result' => $result]);
});
use DrMsigwa\Automation\Models\Execution;
$execution = Execution::find(1);
foreach ($execution->logs as $log) {
echo "[{$log['level']}] {$log['message']}\n";
}
// Only execute flows with specific status
$activeFlows = Flow::where('status', 'active')->get();
foreach ($activeFlows as $flow) {
$runner = new FlowRunner($flow);
$runner->run();
}