Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Charrua's avatar

Run Node or PHP script stored on a database through an API

Hello, I'm trying to store different Node or PHP scripts on a database and run them through an API on demand or through the Laravel scheduler.

The issue here is that I can't figure out how to run the stored script when sending it to the server. I'm imaging a Laravel app sending the request with the script to an API. Once the API gets the script, it executes and send the result (or errors if found).

Would be the same to run some PHP scripts, the important here is for me to understand how the API would run the script (node or PHP) and return some value. I'm really lost.

I have discovered some services that does this, let you create a script and then run it on a schedule or on demand through an API but no clue on how is done.

Thank you.

0 likes
8 replies
automica's avatar

@charrua if I’m following you correctly, you want to hit an api endpoint, execute a php script and then return the result? If that’s the case, you should look at converting your php script into a class and point your api route at it (like writing normal methods in controller).

Alternatively are you attempting to upload arbitrary files, store them in a database and then let a user execute them on your server? If so that sounds pretty risky unless you are able inspect the file and assess there’s nothing malicious in it, you put your site (and server) at risk of attack.

If you could explain why you want to be able to execute these scripts then it will be easier to recommend how.

Charrua's avatar

Hello!

Laravel App

My project idea is to create a Laravel app for in-house use, so the scripts I would store in the database are going to be created by myself. The main goal would be to test some websites frontends through browser automation checks, e.g.

  1. open example.com
  2. click first item
  3. add to cart
  4. check we are on checkout page
  5. take screenshot

For this browser checks I'll be using puppeteer and it runs on Node. So all the scripts that are in charge of testing the browser are going to be saved to the database on the Laravel app.

Then my idea is to create a small Node API that will accept the script, execute and return if some failure or success.

An example of stored Node script for testing using puppeteer would be:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({ path: 'example.png' });

  await browser.close();
})();

Node API

My whole issues are in the Node part, I was already searching and found that the API is easy to mount using Express. At this moment I have the API created and ready to receive a POST request with the script.

But the difficulties are in how to transform or run the script (string) that is received once in the API. I was searching and found that it can be done using a Node child_process.

Further investigation points me to use child_process.fork(modulePath) where modulePath is a string, so I can pass the script stored in the DB and sent through the API.

automica's avatar

So your laravel app is just used as a data store so you can functional test other websites?

This seems overly complicated. My understanding of functional tests is that they should live in the repo of the website you are testing, so that when you run your CI you run the tests relevant to your website. Otherwise, if functionality changes, you need to modify your script, replace the script in your laravel app to reflect the new functionality. Much easier to deploy the fix and the related functional test at the same time and that will ensure you can confirm the new functionality works before you push it out to your live environment.

Charrua's avatar

@automica @snapey

I'm sorry but I missed out a really important part on my explanation, the main goal is to test external websites where I have no control over them.

I agree that the tests must live in the repo of those websites but they do not exist, they are small websites. But some clients wanted to make this type of browser automation and be sure that some parts of their websites are running as they should.

I also know that if some functionality is changed on those websites the tests are going to fail, that's not a problem.

This browser automation would be testing some WordPress and WooCommerce websites, so it will give me useful information when any core/plugin is uploaded and breaks functionality.

The idea on storing the node scripts and send them to the API and return result is inspired on some services as checkly. But I wanted to run it from my own Laravel app.

Remember this is not for testing my own app or my own websites, but testing external websites and be sure some functionality is working as it should.

Thank you.

Snapey's avatar

The suggestion to use dusk still holds. You are launching a headless Chrome browser within your Laravel application and then testing that you can see certain things or interract with the site in some specific way.

I can't think of a reason why these tests would not work against a third party site.

Please or to participate in this conversation.