phantomjs-takescreenshot-action A web service which is implemented as an action to take screenshot of a website using phantomjs

  1. Requirements
  2. Usage

This is a web service which is implemented as an action to take screenshot of a website & return the file content to web service consumer.

For those of you who do not know how to use phantomjs to take screenshot of a web page dynamically, please refer to this article (the "Rendering" example).

Let's assume you have phantomjs installed on server A to take screenshot of a web site. Now you are implementing 2 web applications that need the functionality that server A does. These 2 applications are going to be deployed on server B & C. So, instead of installing phantomjs on server B & C again, a web service is implemented on server A so that you can use this functionality from anywhere. This web service is implemented as a Yii action and will return the content of the screenshot which then can be saved as a jpg image. This web service is going to be called using curl.

Requirements

Yii 1.1 or above

Usage

Copy & paste the extension into protected/extension. Then create an action to set up the web service & another action to call this web service. I implement these 2 actions in 1 controller for simplicity.

<?php
class TestPhantomController extends CController {
	public function actions(){
		return array(
			"takeScreenshot" => array(
				"class" => "ext.phantomjs.EWebScreenshotAction",
				"username" => "username", // username to use this web service
				"password" => "password", // password to use this web service
				"allowedIps" => array(), // restricted to some IPs only, leave blank for any IP
			)
		);
	}
	
	public function actionTestScreenshot() {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->createAbsoluteUrl("/test/takeScreenshot"));
		$fields = array(
			"username" => "username",
			"password" => "password",
			"webUrl" => "http://www.google.com",
		);
		curl_setopt($ch, CURLOPT_POST, count($fields));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
		
		$result = curl_exec($ch);
		$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		
		if ($httpStatus == 200 && ! empty($result)) {
			file_put_contents("C:/test.jpg", $result);
		} else { // Error occurred
			echo $result;
		}
	}
}
?>

Glandore Systems

1 0
4 followers
456 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: hitjuu
Created on: Apr 13, 2012
Last updated: 12 years ago

Downloads

show all

Related Extensions