, (*1)
Laravel PhantomJs Client
Using php-phantomjs client in laravel, (*2)
php-phantomjs Documentation, (*3)
Requirement
Install
Via Composer, (*4)
$ composer require josh/laravel-phantomjs
Config
Add the following provider to providers part of config/app.php, (*5)
Josh\Component\PhantomJs\PhantomJsServiceProvider::class
and the following Facade to the aliases part, (*6)
'PhantomJs' => Josh\Component\PhantomJs\Facade\PhantomJs::class
and then you can run vendor:publish command for generating phantomjs config file
bash
$ php artisan vendor:publish --provider="Josh\Component\PhantomJs\PhantomJsServiceProvider", (*7)
Now you can config your phantomjs client in config/phantomjs.php file
Basic Usage
The following illustrates how to make a basic GET request and output the page content:, (*8)
On Load Finished
// Tells the client to wait for all resources before rendering
$request = \PhantomJs::get('https://www.google.com/');
\PhantomJs::isLazy()->send($request);
// you can use Facade or app make function to use phantomjs
// ex: app('phantomjs') or \PhantomJs
$request = \PhantomJs::get('https://www.google.com/');
$response = \PhantomJs::send($request);
if($response->getStatus() === 200) {
// Dump the requested page content
echo $response->getContent();
}
Saving a screen capture to local disk:, (*9)
$request = \PhantomJs::createImage('https://www.google.com/', 'GET');
$request->setOutputFile(public_path('file.jpg'));
$request->setViewportSize(800, 600);
$request->setCaptureDimensions(800, 600, 0, 0);
$response = \PhantomJs::send($request);
if($response->getStatus() === 200) {
// Dump the requested page content
echo $response->getContent();
}
Outputting a page as PDF:, (*10)
$request = \PhantomJs::createPdf('https://www.google.com/', 'GET');
$request->setOutputFile(public_path('document.pdf'));
$request->setFormat('A4');
$request->setOrientation('landscape');
$request->setMargin('1cm');
$response = \PhantomJs::send($request);
if($response->getStatus() === 200) {
// Dump the requested page content
echo $response->getContent();
}
License
The MIT License (MIT), (*11)