1 | initial version |
To execute Puppeteer from a Laravel Controller using JavaScript code, follow these steps:
npm install puppeteer
Create a new JavaScript file (e.g. puppeteer_script.js) in your project's public folder.
Add the following code to your new script file:
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
//perform operations here
await browser.close();
})();
$cmd = 'node ' . public_path('puppeteer_script.js');
$output = shell_exec($cmd);
This will execute the puppeteer_script.js file in a Node process, running the Puppeteer code and returning any output to your Laravel Controller.
Note: Make sure that you have Node.js installed on your server for this to work.