A
A
Alexander2021-01-26 22:05:26
symfony
Alexander, 2021-01-26 22:05:26

How to return the result (json) from pupetter to the calling PHP script?

According to the crown, you need to update information that is parsed from a third-party site.
The PHP script runs JS with

use Symfony\Component\Process\Process;

            $process = new Process(['node', $file, '--container=' . $container]);
            try {
                $process->mustRun();
                $response = $process->getOutput();
                dump(json_decode($response));

            } catch (ProcessFailedException $e) {
                dump('error', $e->getMessage());
            }

I need to get the json that is returned from the site
doing a function
let result;

(async () => {
    try {
        const browser = await puppeteer.launch({
            headless: false,
            devtools: true
        });
        const page = await browser.newPage();

        const response = await page.goto(url, {waitUntil: ['load', 'domcontentloaded', 'networkidle0', 'networkidle2']});

        const acceptCookies = await Promise.all([
            page.waitForSelector('button.coi-banner__accept'),
            page.click('button.coi-banner__accept'),
        ]);

        await page.type('#trackShipmentSearch', containerNumber);
        await page.keyboard.press(String.fromCharCode(13));

        // теперь включаем перехват запросов
        await page.setRequestInterception(true);
        await page.on('response', async (response) => {
            if (response.url().includes("https://api.maerskline.com/track/")) {
                result = await response.json();
                console.log(result) // JSON RESULT OK
                // здесь перехватил результат, ВСЕ ОК
                await page.evaluate(() => window.stop())
            }
        });

        await console.log(result)// ЗДЕСЬ всегда  undefined
        await browser.close()

    } catch (err) {
        console.log(err)
    }
})();

How can I return json to the calling PHP script?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Derepko, 2021-01-27
@alexmixaylov

https://symfony.com/doc/current/components/process...
https://stackoverflow.com/questions/17822437/pipe-...
Or you can try writing to a separate thread
https://symfony.com/ doc/current/components/process...
https://stackoverflow.com/questions/21491567/how-t...
Either the node can save the result somewhere, and symfony periodically read from there after completion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question