First, install
Mac: First install chromedriver
brew tap homebrew/cask
brew cask install chromedriver
brew cask install chromium
brew cask reinstall chromium
The introduction of packet address: https://github.com/chrome-php/headless-chromium-php
Use common errors:
Exception 'RuntimeException' with message 'Chrome process stopped before startup completed. Additional info: sh: chromium-browser: command not found'
Description: This is because you are disposed inside the introduction of the proper use of the package does not address the following:
$ browserFactory new new BrowserFactory = ( "/ the Applications / Chromium.app / the Contents / MacOS / Chromium");
$ = $ browserFactory- Browser> createBrowser ([ 'noSandbox' => $ Sandbox,
'headless' => to true,
'connectionDelay' => 0.8,
'DebugLogger' => 'PHP: // stdout']);
$ Page = $ browser-> createPage ();
$ Page -> Navigate (URL $) -> waitForReload ();
$ browser-> Close ();
Installation address can refer to:
https://github.com/cpbotha/homebrew-marmaduke-chromium
https://en.wikipedia.org/wiki/Chromium_(web_browser)#Differences_from_Google_Chrome
https://github.com/DomT4/homebrew-chromium
Second, the introduction of the package:
composer require chrome-php/chrome
https://packagist.org/packages/chrome-php/chrome
Third, the use of each method
1, analog browser access
$browserFactory = new BrowserFactory("/Applications/Chromium.app/Contents/MacOS/chromium");
$browser = $browserFactory->createBrowser(['noSandbox' => $sandBox,
'headless' => true,
'connectionDelay' => 0.8,
'debugLogger'=> 'php://stdout']);
$page = $browser->createPage();
$page->navigate($url)->waitForReload();
$browser->close();
2, browser screenshots
$browserFactory = new BrowserFactory("/Applications/Chromium.app/Contents/MacOS/chromium");
$browser = $browserFactory->createBrowser(['noSandbox' => $sandBox,
'headless' => true,
'connectionDelay' => 0.8,
'debugLogger'=> 'php://stdout']);
$page = $browser->createPage();
$page->navigate($url)->waitForReload();
IF ($ SleepTime) { SLEEP (. 6); // wait 6S resource loading and rendering is complete, the script processing need to wait for a general asynchronous }
$ x = 10; $ y = 10; // from the X, Y margins
$ width = 100; $ height = 100; // screenshot width and height
$ clip = new Clip ($ x , $ y, $ width, height $);
$ Screenshot = $ page-> Screenshot ([
'Clip' => $ Clip
]);
$ screenshot-> saveToFile ( 'save-address.jpg'); // save the address of the address
Question: Some shots may appear garbled in Chinese liunx, because the system is the lack of Chinese fonts, you can install yum install cjkuni-uming-fonts cjkuni-ukai-fonts installed system font
Third, generating a pdf file, plus the same behind the front:
$options = [
'landscape' => true, // default to false
'printBackground' => true, // default to false
'displayHeaderFooter' => true, // default to false
'preferCSSPageSize' => true, // default to false ( reads parameters directly from @page )
'marginTop' => 0.0, // defaults to ~0.4 (must be float, value in inches)
'marginBottom' => 1.4, // defaults to ~0.4 (must be float, value in inches)
'marginLeft' => 5.0, // defaults to ~0.4 (must be float, value in inches)
'marginRight' => 1.0 // defaults to ~0.4 (must be float, value in inches)
];
$pdf = $page->pdf($options);
// save the file address
$ pdf-> saveToFile ($ outputImgFullPath) ;
Fourth, the load js:
$page->addScriptTag([
'url' => 'https://code.jquery.com/jquery-3.3.1.min.js'
])->waitForResponse();
$ Page-> evaluate ( '. $ ( ". My.element") html ()'); // calculate the text lines on a given page string