PHP Web Services

PHP Web Services

PHP is available as both a command line tool and web programming language and provides excellent services for communicating using standard SOAP protocols. This article describes how to use a PHP command line script to communicate directly with HEAT LiveTime.

With the widespread popularity of PHP for web programming, many people have expressed interest in writing scripts to communicate with Web Services rather than using more complex languages like Java.

Many script languages now have plugins for working with Web Services. In this example we are going to discuss PHP. Equally, you can achieve the same thing using languages like Perl.

In order to communicate with web services you should have a thorough knowledge of SOAP and also the HEAT LiveTime Web Services API’s. HEAT LiveTime has a developers guide which details each of the API calls and how to use them. What follows are scripts you can use to handle some of the more difficult parts of communication. Notably session management.

The following code assumes that PHP is installed and running with the PHP SOAP extension. You will note how the cookie is used to manage session state. Feel free to use these functions in your own scripts to facilitate rapid protoyping of your own solutions to communicate directly with HEAT LiveTime.

<?php
	/*
	  HEAT LiveTime example PHP command line script for web services
	  Requires the PHP SOAP extension to operate
	*/

	$hostAddress = "localhost";
	$baseProduct = "LiveTime";
	$baseAddress = "/" . $baseProduct . "/WebObjects/" . $baseProduct . ".woa/ws/";
	$baseService = "http://" . $hostAddress . $baseAddress;

	function internalLogin($username, $password) {
		global $objClient;
		// Note that setting "trace" will allow us to view the 
		// request that we are making, after we have made it.

		// Connect to HEAT LiveTime Authenticate?wsdl
		// (Use only supervisors / team leaders / technicians)
		$results = $objClient->connect($username, $password);

		//This will return an Associative Array
		$success = $results['success'];
		if ($success=="false") {
			echo $results['message'] . "\n";
			exit();
		}
		echo "Login Successful\n";
		createSession();
	}

	function ldapLogin($ldapSourceId, $username, $password) {
		global $objClient;
		// Note that setting "trace" will allow us to view the 
		// request that we are making, after we have made it.

		// Connect to HEAT LiveTime Authenticate?wsdl
		// (Use only supervisors / team leaders / technicians)
		$results = $objClient->connect($ldapSourceId, $username, $password);

		//This will return an Associative Array
		$success = $results['success'];
		if ($success=="false") {
			echo $results['message'] . "\n";
			exit();
		}
		echo "Login Successful\n";
		createSession();
	}

	function logout() {
		global $objClient;
		global $baseService;

		$objClient->SoapClient($baseService . "Authenticate?wsdl", 
				array('trace' => true));

		$results = $objClient->disconnect();
		//This will return an Associative Array
		$success = $results['success'];
		if ($success=="false") {
			echo $results['message'] . "\n";
			exit();
		}
		echo "Logout Successful\n";
		exit();
	}

	function createSession() {
		global $objClient;
		// Retrieve the Last Response Header from the HEAT LiveTime server
		$responseHeader = $objClient->__getLastResponseHeaders();

		$sessionPos=strpos($responseHeader, "JSESSIONID=");

		if ($sessionPos === false) {
			echo "No session id was found. Exiting.\n";
			exit();
		}
		// cookie will always be 32 bytes
		$cookie = substr($responseHeader,$sessionPos + 11,32);

		// Set the Cookie name for the next request
		$objClient->__setCookie("JSESSIONID", $cookie);
	}

	function findUser($username,$email,$first,$last) {
		global $objClient;
		global $baseService;

		// switch to the customer api
		$objClient->SoapClient($baseService . "Customer?wsdl", 
				array('trace' => true));

		echo "Searching for " . $username . $email . $first . $last . "\n";
		// Call findCustomer, using an email address and 
		// echo out response (username, email, first, last)
		$results = $objClient->findCustomer($username,$email,$first,$last);

		$success = $results['success'];
		if ($success=="false") {
			echo $results['message'] . "\n";
			return;
		}

		// if we have results lets show them (returns an array of arrays)
		foreach(array_values($results) as $ess) {
			print_r($ess);
		}
	}

	// construct the soap client to create a connection and then login
	// trace is for debugging, soap 1.1 is to allow overloaded methods
	$objClient = new SoapClient($baseService . "Authenticate?wsdl", 
		array('trace' => true, 'soap_version' => 'SOAP_1_1'));

	// Depending on your chosen login configuration...

	// For internally managed logins (usernames and passwords in HEAT LiveTime)
	// internalLogin("super","super");


	// For ldap managed logins (authentication is delegated to an AD/LDAP Server)
	// $knownLdapSourceId = "1";
	// $knownLdapUsername = "ldapUsername"; // No domain detail, source defines it
	// $knownLdapPassword = "ldapPassword";
	// ldapLogin($knownLdapSourceId, $knownLdapUsername, $knownLdapPassword);


	// Assuming authentication was successful...
	findUser("","some_email@_mycompany.com","","");

	// We are all done, disconnect...
	logout();
?>

In the example function above we first login using the connect method of the HEAT LiveTime Authenticate wsdl and this subsequently calls the createSession function. To maintain session we grab the JSESSIONID from the response header and then extract the 32 byte cookie. We can now use this cookie in subsequent web service calls to HEAT LiveTime. The complete sample code is available for download. Do not forget to logout after completing all the tasks in your code.

Once you have developed your first few scripts you will see that it is possible to do anything you like with the data you obtain from HEAT LiveTime. This can be used for coordinating information from multiple data stores or live population of the CMDB from custom systems.