clienttest.php revision b7307c9c040a1a8aed0dbabed6574466d0bff2e4
4169N/A<?php
1178N/A
1178N/A/*
1178N/A * Sample client for the VirtualBox webservice, written in PHP.
1178N/A *
1178N/A * Run the VirtualBox web service server first; see the VirtualBox
1178N/A * SDK reference for details.
1178N/A *
1178N/A * Copyright (C) 2009-2010 Oracle Corporation
1178N/A * Contributed by James Lucas (mjlucas at eng.uts.edu.au).
1178N/A *
1178N/A * The following license applies to this file only:
1178N/A *
1178N/A * Permission is hereby granted, free of charge, to any person
1178N/A * obtaining a copy of this software and associated documentation
1178N/A * files (the "Software"), to deal in the Software without
1178N/A * restriction, including without limitation the rights to use,
2362N/A * copy, modify, merge, publish, distribute, sublicense, and/or
2362N/A * sell copies of the Software, and to permit persons to whom the
2362N/A * Software is furnished to do so, subject to the following conditions:
1178N/A *
5176N/A * The above copyright notice and this permission notice shall be
1178N/A * included in all copies or substantial portions of the Software.
1178N/A *
4033N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
4935N/A * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0N/A * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0N/A * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
4935N/A * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0N/A * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
4935N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0N/A * OTHER DEALINGS IN THE SOFTWARE.
5176N/A */
0N/A
0N/Arequire_once('./vboxServiceWrappers.php");
4033N/A
0N/A//Connect to webservice
0N/A$connection = new SoapClient("vboxwebService.wsdl", array('location" => "http://localhost:18083/"));
0N/A
0N/A//Logon to webservice
4935N/A$websessionManager = new IWebsessionManager($connection);
0N/A// Dummy username and password (change to appropriate values or set authentication method to null)
0N/A$virtualbox = $websessionManager->logon("username","password");
4935N/A
0N/A//Get a list of registered machines
0N/A$machines = $virtualbox->machines;
4935N/A
0N/A//Take a screenshot of the first vm we find that is running
0N/Aforeach ($machines as $machine)
4935N/A{
0N/A if ( 'Running" == $machine->state )
0N/A {
4935N/A $session = $websessionManager->getSessionObject($virtualbox->handle);
0N/A $uuid = $machine->id;
0N/A $machine->lockMachine($session->handle, "Shared");
0N/A try
4935N/A {
1178N/A $console = $session->console;
0N/A $display = $console->display;
4935N/A list($screenWidth, $screenHeight, $screenBpp, $screenX, $screenY, $screenStatus) = $display->getScreenResolution(0 /* First screen */);
0N/A
0N/A $imageraw = $display->takeScreenShotToArray(0 /* First screen */, $screenWidth, $screenHeight, "RGBA");
4935N/A echo "Screenshot size: " . sizeof($imageraw) . "\n";
0N/A
0N/A $filename = 'screenshot.png";
4935N/A echo "Saving screenshot of " . $machine->name . " (${screenWidth}x${screenHeight}, ${screenBpp}BPP) to $filename\n";
1178N/A $image = imagecreatetruecolor($screenWidth, $screenHeight);
4935N/A
1178N/A for ($height = 0; $height < $screenHeight; $height++)
0N/A {
4935N/A for ($width = 0; $width < $screenWidth; $width++)
1178N/A {
1178N/A $start = ($height*$screenWidth + $width)*4;
4935N/A $red = $imageraw[$start];
0N/A $green = $imageraw[($start+1)];
0N/A $blue = $imageraw[($start+2)];
0N/A //$alpha = $image[$start+3];
0N/A
0N/A $colour = imagecolorallocate($image, $red, $green, $blue);
0N/A
0N/A imagesetpixel($image, $width, $height, $colour);
0N/A }
0N/A }
0N/A
0N/A imagepng($image, $filename);
4935N/A }
0N/A catch (Exception $ex)
0N/A {
0N/A echo $ex->getMessage();
0N/A }
0N/A
0N/A $session->unlockMachine();
0N/A
0N/A $machine->releaseRemote();
0N/A $session->releaseRemote();
0N/A
0N/A break;
4935N/A }
0N/A}
0N/A
0N/A$websessionManager->logoff($virtualbox->handle);
0N/A
0N/A?>
0N/A
0N/A