diff --git a/README.md b/README.md index 9c41a74..eb96d04 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ The following illustrates how to make a basic GET request and output the page co // you can use Facade or app make function to use phantomjs // ex: app('phantomjs') or \PhantomJs -$response = \PhantomJs::get('http://google.com'); +$request = \PhantomJs::get('http://phantomjs.org/'); + +$response = \PhantomJs::send($request); if($response->getStatus() === 200) { @@ -60,38 +62,39 @@ if($response->getStatus() === 200) { Saving a screen capture to local disk: ```php -$width = 800; -$height = 600; -$top = 0; -$left = 0; - -$request = \PhantomJs::getMessageFactory()->createCaptureRequest('http://google.com', 'GET'); +$request = \PhantomJs::createImage('http://phantomjs.org/', 'GET'); $request->setOutputFile(public_path('file.jpg')); -$request->setViewportSize($width, $height); +$request->setViewportSize(800, 600); -$request->setCaptureDimensions($width, $height, $top, $left); +$request->setCaptureDimensions(800, 600, 0, 0); -$response = \PhantomJs::getMessageFactory()->createResponse(); +$response = \PhantomJs::send($request); + +if($response->getStatus() === 200) { -\PhantomJs::send($request, $response); + // Dump the requested page content + echo $response->getContent(); +} ``` Outputting a page as PDF: ```php -use Josh\Component\PhantomJs\PhantomJs; - -$request = PhantomJs::getMessageFactory()->createPdfRequest('http://google.com', 'GET'); +$request = \PhantomJs::createPdf('http://phantomjs.org/', 'GET'); $request->setOutputFile(public_path('document.pdf')); $request->setFormat('A4'); $request->setOrientation('landscape'); $request->setMargin('1cm'); -$response = PhantomJs::getMessageFactory()->createResponse(); +$response = \PhantomJs::send($request); + +if($response->getStatus() === 200) { -PhantomJs::send($request, $response); + // Dump the requested page content + echo $response->getContent(); +} ``` ## License diff --git a/src/Facade/PhantomJs.php b/src/Facade/PhantomJs.php index eec5dac..a78d243 100644 --- a/src/Facade/PhantomJs.php +++ b/src/Facade/PhantomJs.php @@ -4,6 +4,9 @@ use JonnyW\PhantomJs\Engine; use Illuminate\Support\Facades\Facade; +use JonnyW\PhantomJs\Http\CaptureRequest; +use JonnyW\PhantomJs\Http\PdfRequest; +use JonnyW\PhantomJs\Http\Request; use JonnyW\PhantomJs\Http\RequestInterface; use JonnyW\PhantomJs\Http\ResponseInterface; use JonnyW\PhantomJs\Http\MessageFactoryInterface; @@ -20,6 +23,13 @@ * @method static ProcedureLoaderInterface getProcedureLoader * @method static ProcedureCompilerInterface getProcedureCompiler * @method static ResponseInterface send(RequestInterface $request, ResponseInterface $response) + * @method static PdfRequest createPdf(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = []) + * @method static CaptureRequest createImage(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = []) + * @method static CaptureRequest request(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = []) + * @method static Request get(string $url, array $headers = [], array $parameters = []) + * @method static Request post(string $url, array $headers = [], array $parameters = []) + * @method static Request put(string $url, array $headers = [], array $parameters = []) + * @method static Request delete(string $url, array $headers = [], array $parameters = []) */ class PhantomJs extends Facade { diff --git a/src/PhantomJs.php b/src/PhantomJs.php index 5ec2cd3..9494cbd 100644 --- a/src/PhantomJs.php +++ b/src/PhantomJs.php @@ -5,7 +5,12 @@ use Illuminate\Support\Arr; use JonnyW\PhantomJs\Client; use JonnyW\PhantomJs\Engine; +use JonnyW\PhantomJs\Http\Request; +use JonnyW\PhantomJs\Http\Response; +use JonnyW\PhantomJs\Http\PdfRequest; +use JonnyW\PhantomJs\Http\CaptureRequest; use JonnyW\PhantomJs\Http\RequestInterface; +use JonnyW\PhantomJs\Http\ResponseInterface; class PhantomJs { @@ -31,7 +36,7 @@ class PhantomJs */ public function __construct($options = []) { - $this->engine = new Engine(); + $this->engine = new Engine; $this->setOptions($options); $this->container = $this->getContainer(); } @@ -42,7 +47,7 @@ public function __construct($options = []) * @param $path * @return $this */ - public function setBinaryPath($path) + public function setBinaryPath($path) : PhantomJs { $this->engine->setPath($path); @@ -55,7 +60,7 @@ public function setBinaryPath($path) * @param $debug * @return $this */ - public function setDebug($debug) + public function setDebug($debug) : PhantomJs { $this->engine->debug($debug); @@ -68,7 +73,7 @@ public function setDebug($debug) * @param $cache * @return $this */ - public function setCache($cache) + public function setCache($cache) : PhantomJs { $this->engine->cache($cache); @@ -78,9 +83,9 @@ public function setCache($cache) /** * Get engine object of phantomjs client * - * @return \Illuminate\Foundation\Application|mixed + * @return Engine */ - public function getEngine() + public function getEngine() : Engine { return $this->engine; } @@ -90,7 +95,7 @@ public function getEngine() * * @return PhantomJsServiceContainer */ - public function getContainer() + public function getContainer() : PhantomJsServiceContainer { $serviceContainer = PhantomJsServiceContainer::getInstance(); @@ -104,7 +109,7 @@ public function getContainer() * * @return \Exception|Client */ - public function getClient() + public function getClient() : Client { try { @@ -147,37 +152,66 @@ public function setOptions(array $options) } /** - * Create request + * send request * * @param $url - * @param string $method + * @param $method + * @param $timeout * @param array $headers - * @param array $data + * @param array $parameters + * @return Request + */ + public function request(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : Request + { + $request = new Request($url, $method, $timeout); + + $request->setHeaders($headers); + + $request->setRequestData($parameters); + + return $request; + } + + /** + * Create pdf request + * + * @param $url + * @param $method * @param int $timeout - * @return RequestInterface + * @param array $headers + * @param array $parameters + * @return PdfRequest */ - public function createRequest($url, $method = RequestInterface::METHOD_GET,$headers = [],$data = [], $timeout = 5000) + public function createPdf(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : PdfRequest { - $request = $this->getClient()->getMessageFactory()->createRequest($url, $method, $timeout); + $request = new PdfRequest($url, $method, $timeout); $request->setHeaders($headers); - $request->setRequestData($data); + $request->setRequestData($parameters); return $request; } /** - * Get response of request + * Create pdf request * - * @param $request - * @return \JonnyW\PhantomJs\Http\ResponseInterface + * @param $url + * @param $method + * @param int $timeout + * @param array $headers + * @param array $parameters + * @return CaptureRequest */ - public function createResponse($request) + public function createImage(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : CaptureRequest { - $response = $this->getClient()->getMessageFactory()->createResponse(); + $request = new CaptureRequest($url, $method, $timeout); + + $request->setHeaders($headers); + + $request->setRequestData($parameters); - return $this->getClient()->send($request, $response); + return $request; } /** @@ -186,13 +220,11 @@ public function createResponse($request) * @param $url * @param array $headers * @param array $parameters - * @return \JonnyW\PhantomJs\Http\ResponseInterface + * @return Request */ - public function get($url,$headers = [],$parameters = []) + public function get(string $url, array $headers = [], array $parameters = []) : Request { - $request = $this->createRequest($url, 'GET', $headers, $parameters); - - return $this->createResponse($request); + return $this->request($url, RequestInterface::METHOD_GET, 5000, $headers, $parameters); } /** @@ -201,13 +233,11 @@ public function get($url,$headers = [],$parameters = []) * @param $url * @param array $headers * @param array $parameters - * @return \JonnyW\PhantomJs\Http\ResponseInterface + * @return Request */ - public function post($url, $headers = [], $parameters = []) + public function post(string $url, array $headers = [], array $parameters = []) : Request { - $request = $this->createRequest($url, 'POST', $headers, $parameters); - - return $this->createResponse($request); + return $this->request($url, RequestInterface::METHOD_POST, 5000, $headers, $parameters); } /** @@ -216,13 +246,11 @@ public function post($url, $headers = [], $parameters = []) * @param $url * @param array $headers * @param array $parameters - * @return \JonnyW\PhantomJs\Http\ResponseInterface + * @return Request */ - public function put($url, $headers = [], $parameters = []) + public function put(string $url, array $headers = [], array $parameters = []) : Request { - $request = $this->createRequest($url, 'PUT', $headers, $parameters); - - return $this->createResponse($request); + return $this->request($url, RequestInterface::METHOD_PUT, 5000, $headers, $parameters); } /** @@ -231,12 +259,22 @@ public function put($url, $headers = [], $parameters = []) * @param $url * @param array $headers * @param array $parameters - * @return \JonnyW\PhantomJs\Http\ResponseInterface + * @return Request */ - public function delete($url, $headers = [], $parameters = []) + public function delete(string $url, array $headers = [], array $parameters = []) : Request { - $request = $this->createRequest($url, 'PUT', $headers, $parameters); + return $this->request($url, RequestInterface::METHOD_DELETE, 5000, $headers, $parameters); + } - return $this->createResponse($request); + /** + * Send request with response + * + * @param RequestInterface $request + * @param ResponseInterface $response + * @return ResponseInterface + */ + public function send(RequestInterface $request, ResponseInterface $response = null) : ResponseInterface + { + return $this->getClient()->send($request, ( is_null($response) ? new Response : $response ) ); } } \ No newline at end of file diff --git a/src/PhantomJsServiceProvider.php b/src/PhantomJsServiceProvider.php index 722a675..855014e 100644 --- a/src/PhantomJsServiceProvider.php +++ b/src/PhantomJsServiceProvider.php @@ -14,7 +14,7 @@ class PhantomJsServiceProvider extends ServiceProvider * @since 7 May 2017 * @return void */ - public function boot() + public function boot() : void { $this->publishes([ __DIR__ . '/../config.php' => config_path( 'phantomjs.php' ) ]); } @@ -26,7 +26,7 @@ public function boot() * @since 7 May 2017 * @return void */ - public function register() + public function register() : void { $this->app->singleton('phantomjs', function(){ @@ -41,9 +41,9 @@ public function register() * @since 8 May 2017 * @return PhantomJs */ - protected function getClient() + protected function getClient() : PhantomJs { - $client = app(PhantomJs::class); + $client = new PhantomJs; if(file_exists(config_path('phantomjs.php'))){ $config = config('phantomjs'); diff --git a/tests/LaravelPhantomJsTest.php b/tests/LaravelPhantomJsTest.php deleted file mode 100644 index 04b83af..0000000 --- a/tests/LaravelPhantomJsTest.php +++ /dev/null @@ -1,17 +0,0 @@ -phantomjs->get('https://google.com'); - $this->assertEquals('https://google.com/', $response->getUrl()); - } -} \ No newline at end of file diff --git a/tests/PhantomJsRequestTest.php b/tests/PhantomJsRequestTest.php new file mode 100644 index 0000000..fa725b6 --- /dev/null +++ b/tests/PhantomJsRequestTest.php @@ -0,0 +1,66 @@ + + * @since 12 Feb, 2018 + * @throws \Exception + */ + public function testSimpleRequest() + { + $request = $this->phantomjs->get('http://phantomjs.org/'); + + $response = $this->phantomjs->send($request); + + $this->assertEquals($response->getUrl(),'http://phantomjs.org/'); + $this->assertEquals($response->getStatus(),200); + } + + /** + * Test create pdf from request + * + * @author Alireza Josheghani + * @since 12 Feb, 2018 + * @throws \Exception + * @throws \JonnyW\PhantomJs\Exception\NotWritableException + */ + public function testCreatePdf() + { + $request = $this->phantomjs->createPdf('http://phantomjs.org/'); + $request->setOutputFile($filename = str_random(30) . '.pdf'); + $request->setFormat('A4'); + $request->setOrientation('landscape'); + $request->setMargin('1cm'); + + $this->phantomjs->send($request); + + $this->assertFileExists($filename); + unlink($filename); + } + + /** + * Test create image from request + * + * @author Alireza Josheghani + * @since 12 Feb, 2018 + * @throws \JonnyW\PhantomJs\Exception\NotWritableException + * @throws \Exception + */ + public function testCreateImage() + { + $request = $this->phantomjs->createImage('http://phantomjs.org/'); + $request->setOutputFile($filename = str_random(30) . '.png'); + $request->setViewportSize(800, 600); + $request->setCaptureDimensions(800, 600, 0, 0); + + $this->phantomjs->send($request); + + $this->assertFileExists($filename); + unlink($filename); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 65c9fea..931c166 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -15,7 +15,7 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->phantomjs = new PhantomJs([ - 'binary_path' => env('PHANTOMJS_BINARY_PATH') + 'binary_path' => env('PHANTOMJS_BINARY_PATH', '/usr/local/bin/phantomjs') ]); } } \ No newline at end of file