DeveloperJoy Logo DeveloperJoy

PuPHPEteer: Bringing Puppeteer to PHP

July 6, 2024
PuPHPEteer: Bringing Puppeteer to PHP

PuPHPEteer is a powerful bridge that connects the world of Puppeteer with PHP, enabling developers to leverage the extensive capabilities of Puppeteer within a PHP environment.

This tool is built on top of Rialto, a package designed to manage Node resources from PHP.

Key Features

  • Full Puppeteer API Support: PuPHPEteer fully supports the Puppeteer API, allowing you to use it just as you would in a Node.js environment.
  • Seamless Integration with PHP: By adapting Puppeteer’s functionality to PHP's syntax, PuPHPEteer provides a familiar and comfortable interface for PHP developers.
  • Robust Node Management: Built on Rialto, PuPHPEteer effectively manages Node.js resources, ensuring optimal performance and reliability.

Installation

PuPHPEteer can be installed via Composer. Make sure you have PHP and Node.js installed on your system.

composer require zoon/puphpeteer

Basic Usage

PuPHPEteer can be used to perform a variety of tasks, such as navigating to web pages, taking screenshots, and executing scripts within the context of a web page. Below are a couple of examples demonstrating its usage.

Example 1: Navigating and Taking a Screenshot

The following example demonstrates how to navigate to a web page and save a screenshot.

use Nesk\Puphpeteer\Puppeteer;

$puppeteer = new Puppeteer;
$browser = $puppeteer->launch();

$page = $browser->newPage();
$page->goto('https://example.com');
$page->screenshot(['path' => 'example.png']);

$browser->close();

Example 2: Evaluating a Script

This example shows how to evaluate a script in the context of a page.

use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;

$puppeteer = new Puppeteer;

$browser = $puppeteer->launch();
$page = $browser->newPage();
$page->goto('https://example.com');

// Get the "viewport" of the page, as reported by the page.
$dimensions = $page->evaluate(JsFunction::createWithBody("
    return {
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        deviceScaleFactor: window.devicePixelRatio
    };
"));

echo 'Dimensions: ' . json_encode($dimensions);

$browser->close();

Advanced Usage

Beyond basic navigation and script evaluation, PuPHPEteer supports more advanced functionalities such as handling authentication, manipulating the DOM, and interacting with various elements on a page.

Handling Authentication

You can use PuPHPEteer to handle basic authentication.

$page->authenticate([
    'username' => 'myUsername',
    'password' => 'myPassword'
]);
$page->goto('https://example.com/secure');

Manipulating the DOM

Interact with elements on the page by using methods like $page->click(), $page->type(), and more.

$page->goto('https://example.com');
$page->type('#search', 'puppeteer');
$page->click('#search_button');
$page->waitForNavigation();

Extract data from the website

After loading a website, you have access to all elements in DOM.

You can get information using querySelector, or querySelectorAll. Here is a simple example:

$company_elements = $page->querySelectorAll('.company');

foreach ($company_elements as $company_element) {
  // select the name and address elements
  $name_element = $company_element->querySelector('h2');
  $address_element = $company_element->querySelector('p');

  // retrieve the data of interest
  $name = $name_element->evaluate(JsFunction::createWithParameters(['node'])->body('return node.innerText;'));
  $address = $address_element->evaluate(JsFunction::createWithParameters(['node'])->body('return node.innerText;'));

  // create a new company object and add it to the list
  $company = ['name' => $name, 'address' => $address];
  $companies[] = $company;
}

PuPHPEteer extends the powerful capabilities of Puppeteer to PHP developers, enabling them to automate web tasks efficiently within their preferred programming environment. Whether it's for testing, web scraping, or automation, PuPHPEteer offers a versatile and robust solution for your PHP applications.

For more information, visit the official repository.

By leveraging PuPHPEteer, developers can harness the full power of Puppeteer while working within a PHP ecosystem, making it a valuable tool for a wide range of web automation tasks.

← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995

/m

What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995

/m

Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.

FAQ