Download the PHP package fagundes/chrome without Composer
On this page you can find all versions of the php package fagundes/chrome. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fagundes/chrome
More information about fagundes/chrome
Files in fagundes/chrome
Package chrome
Short Description Instrument headless chrome/chromium instances from php
License Fair
Homepage https://github.com/gsouf/headless-chromium-php
Informations about the package chrome
Chrome PHP
This library lets you start playing with chrome/chromium in headless mode from PHP.
/!\ The library is still young and some features you need might be missing. We add feature as feature requests are submitted, feel free to rise an issue if you want to see a new feature to be supported by the library. Additionally the library follows semver. That means that until version 1.0.0 a lot of changes might occur.
⚠️Loking for maintainers⚠️
Many people are showing interest in the library but I don't have much time to maintain it. I'm looking for some permanent maintainers to help with it.
It can be for handling issues, adding documentation, adding new features, fixing bugs, etc...
Features
- Open chrome or chromium browser from php
- Create pages and navigate to pages
- Take screenshots
- Evaluate javascript in the page
- Make PDF
- Emulate mouse
- TODO Emulate keyboard
- Always IDE friendly
Happy browsing!
Requirements
Requires php 7 and a chrome/chromium executable.
As of version 65 of chrome/chromium the library proved to work correctly. Please try to keep using latest version of chrome.
Note that the library is only tested on linux but is compatible with osX and windows.
Install
The library can be installed with composer and is available on packagist under chrome-php/chrome
composer require chrome-php/chrome
Usage
It uses a simple and understandable API to start chrome, to open pages, to take screenshots, to crawl websites... and almost everything that you can do with chrome as a human.
Using different chrome executable
When starting the factory will look for the environment variable "CHROME_PATH"
to find the chrome executable.
If the variable is not found then it will use "chrome"
as the executable.
You can use any executable of your choice. For instance "chromium-browser"
:
Debugging
The following example adds some development-oriented features to help debugging
About debugLogger
: this can be any of a resource string, a resource or an object implementing
LoggerInterface
from Psr\Log (such as monolog
or apix/log).
API
Browser Factory
Options
Here are the options available for the browser factory:
Option name | Default | Description |
---|---|---|
connectionDelay | 0 | Delay to apply between each operation for debugging purposes |
customFlags | none | Array of flags to pass to the command line. Eg: ['--option1', '--option2=someValue'] |
debugLogger | null | A string (e.g "php://stdout"), or resource, or PSR-3 logger instance to print debug messages |
enableImages | true | Toggles loading of images |
headless | true | Enable or disable headless mode |
ignoreCertificateErrors | false | Set chrome to ignore ssl errors |
keepAlive | true | true to keep alive the chrome instance when the script terminates |
noSandbox | false | Useful to run in a docker container |
sendSyncDefaultTimeout | 3000 | Default timeout (ms) for sending sync messages |
startupTimeout | 30 | Maximum time in seconds to wait for chrome to start |
userAgent | none | User agent to use for the whole browser (see page api for alternative) |
userDataDir | none | chrome user data dir (default: a new empty dir is generated temporarily) |
windowSize | none | Size of the window. usage: [$width, $height] - see also Page::setViewport |
Browser API
Create a new page (tab)
Close the browser
Set a script to evaluate before every page created by this browser will navigate
Page API
Navigate to an url
When Using $navigation->waitForNavigation()
you will wait for 30sec until the page event "loaded" is triggered.
You can change the timeout or the event to listen for:
Available events (in the order they trigger):
Page::DOM_CONTENT_LOADED
: dom has completely loadedPage::LOAD
: (default) page and all resources are loadedPage::NETWORK_IDLE
: page has loaded, and no network activity has occurred for at least 500ms
When you want to wait for the page to navigate there are 2 main issues that may occur. First the page is too long to load and second the page you were waiting to be loaded has been replaced. The good news is that you can handle those issues using a good old try catch:
Evaluate script on the page
Once the page has completed the navigation you can evaluate arbitrary script on this page:
Sometime the script you evaluate will click a link or submit a form, in this case the page will reload and you will want to wait for the new page to reload.
You can achieve this by using $page->evaluate('some js that will reload the page')->waitForPageReload()
.
An example is available in form-submit.php
Call a function
This is an alternative to evaluate
that allows to call a given function with the given arguments in the page context:
Add a script tag
That's useful if you want to add jQuery (or anything else) to the page:
You can also use an url to feed the src attribute:
Add a script to evaluate upon page navigation
If your script needs the dom to be fully populated before it runs then you can use the option "onLoad":
Set viewport size
This features allows to change the size of the viewport (emulation) for the current page without affecting the size of
all the browser's pages (see also option "windowSize"
of BrowserFactory::createBrowser).
Make a screenshot
choose an area
You can use the option "clip" in order to choose an area for the screenshot (TODO exemple)
take a full page screenshot
You can also take a screenshot for the full layout (not only the layout) using $page->getFullPageClip
(TODO exemple)
TODO Page.getFullPageClip();
Print as PDF
Options headerTempalte
and footerTempalte
:
Should be valid HTML markup with following classes used to inject printing values into them:
- date: formatted print date
- title: document title
- url: document location
- pageNumber: current page number
- totalPages: total pages in the document
Mouse API
The mouse API is dependent on the page instance and allows you to control the mouse's moves and clicks.
Cookie API
You can set and get cookies for a page:
Set Cookie
Get Cookies
Set user agent
You can set an user agent per page :
See also BrowserFactory option userAgent
to set it for the whole browser.
Advanced usage
The library ships with tools that hide all the communication logic but you can use the tools used internally to communicate directly with chrome debug protocol.
Example:
Create a session and send message to the target
Debugging
You can ease the debugging by setting a delay before each operation is made:
Browser (standalone)
Contributing
See CONTRIBUTING.md for contribution details.
Credits
Thanks to puppeteer that served as an inspiration.
Roadmap
- Make pdf
- Create a DOM manipulation framework
- Inspect network traces
- Emulate keyboard
- Adding api documentation (https://github.com/victorjonsson/PHP-Markdown-Documentation-Generator/blob/master/docs.md)
Authors
- Soufiane Ghzal - Initial work - gsouf
See also the list of contributors who participated in this project.
License
This project is licensed under the Fair License.
All versions of chrome with dependencies
symfony/process Version ^3|^4|^5
wrench/wrench Version ^2.0
symfony/filesystem Version ^3|^4|^5
psr/log Version ^1.0
apix/log Version ^1.2
evenement/evenement Version ^3.0.1