项目作者: mrjosh

项目描述 :
🏏 Use phantom-js in laravel
高级语言: PHP
项目地址: git://github.com/mrjosh/laravel-phantomjs.git
创建时间: 2017-05-08T10:01:48Z
项目社区:https://github.com/mrjosh/laravel-phantomjs

开源协议:

下载


Build Status
Latest Stable Version
Total Downloads
Latest Unstable Version
Discord
License

Laravel PhantomJs Client

Using php-phantomjs client in laravel

php-phantomjs Documentation

Requirement

Install

Via Composer

  1. $ composer require josh/laravel-phantomjs

Config

Add the following provider to providers part of config/app.php

  1. Josh\Component\PhantomJs\PhantomJsServiceProvider::class

and the following Facade to the aliases part

  1. 'PhantomJs' => Josh\Component\PhantomJs\Facade\PhantomJs::class

and then you can run vendor:publish command for generating phantomjs config file

  1. $ php artisan vendor:publish --provider="Josh\Component\PhantomJs\PhantomJsServiceProvider"

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:

On Load Finished

  1. // Tells the client to wait for all resources before rendering
  2. $request = \PhantomJs::get('https://www.google.com/');
  3. \PhantomJs::isLazy()->send($request);
  1. // you can use Facade or app make function to use phantomjs
  2. // ex: app('phantomjs') or \PhantomJs
  3. $request = \PhantomJs::get('https://www.google.com/');
  4. $response = \PhantomJs::send($request);
  5. if($response->getStatus() === 200) {
  6. // Dump the requested page content
  7. echo $response->getContent();
  8. }

Saving a screen capture to local disk:

  1. $request = \PhantomJs::createImage('https://www.google.com/', 'GET');
  2. $request->setOutputFile(public_path('file.jpg'));
  3. $request->setViewportSize(800, 600);
  4. $request->setCaptureDimensions(800, 600, 0, 0);
  5. $response = \PhantomJs::send($request);
  6. if($response->getStatus() === 200) {
  7. // Dump the requested page content
  8. echo $response->getContent();
  9. }

Outputting a page as PDF:

  1. $request = \PhantomJs::createPdf('https://www.google.com/', 'GET');
  2. $request->setOutputFile(public_path('document.pdf'));
  3. $request->setFormat('A4');
  4. $request->setOrientation('landscape');
  5. $request->setMargin('1cm');
  6. $response = \PhantomJs::send($request);
  7. if($response->getStatus() === 200) {
  8. // Dump the requested page content
  9. echo $response->getContent();
  10. }

License

The MIT License (MIT)