1.11 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 Service Desk.

With the widespread popularity of PHP for web programming, many people have expressed interest in writing scripts to communicate with Service Desk’s 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 Service Desk’s web services you should have a thorough knowledge of SOAP and also Service Desk’s Web Services API’s. Service Desk 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 prototyping of your own solutions to communicate directly with Service Desk.

<?php /* LiveTime example PHP command line script for web services Requires the PHP SOAP extension to operate */ $hostAddress = "www.host.com"; $baseProduct = "LiveTime"; $baseAddress = "/" . $baseProduct . "/WebObjects/" . $baseProduct . ".woa/ws/"; $baseService = "http://" . $hostAddress . $baseAddress; function login($username,$password) { global $objClient; /* Setting "trace" will allow us to view the request that we are making, after we have made it. */ # Connect to LiveTime Authenticate?wsdl # Use only technicians or supervisors $results = $objClient->connect($username, $password); //This will return an Associative Array $success = $results['success']; if ($success=="false") { echo $results['message'] . "n"; exit(); } echo "Login Successfuln"; 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 Successfuln"; exit(); } function createSession() { global $objClient; # # Retrieve the Last Response Header from the 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 $objClient = new SoapClient($baseService . "Authenticate?wsdl", array('trace' => true)); //if using AD or LDAP you need to enter the Fully Qualified AD/LDAP Domain //super@domain.mycompany.com login("super","super"); findUser("","cbrown","",""); //we are all done logout(); ?>

In the example function above we first login using the connect method of Service Desk’s 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 Service Desk. 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 Service Desk. This can be used for coordinating information from multiple data stores or live population of the CMDB from custom systems.