Ask Your Question
0

How can Puppeteer be executed from a Laravel Controller using JavaScript code?

asked 2023-06-09 08:13:34 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-09 08:20:02 +0000

nofretete gravatar image

To execute Puppeteer from a Laravel Controller using JavaScript code, follow these steps:

  1. Install Puppeteer package by running the following command in your Laravel project directory:
npm install puppeteer
  1. Create a new JavaScript file (e.g. puppeteer_script.js) in your project's public folder.

  2. 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();
})();
  1. In your Laravel Controller's method, add the following line to execute the puppeteer_script.js file:
$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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-09 08:13:34 +0000

Seen: 23 times

Last updated: Jun 09 '23