OTRS – TicketGet() Web Service Example in PHP

Posted on March 5, 2013

UPDATED:  I’ve changed employers and have moved on to other projects.  I no longer use OTRS, or have access to OTRS, so I won’t really be able to help you beyond what I’ve already posted here. 

Jeff

Continuing on with my studies of OTRS web services, I’ve created a page that will pull an OTRS ticket back when supplied with the ticket’s ID via GET.  Prior to this I posted how to create a ticket using the OTRS TicketCreate() SOAP function. Basically, you give the page the ticket ID via the URL – http://webserver/get_ticket.php?id=xx. I’ve included a complete ticketGet zip file at the end.  In addition to this example, I have another post explaining how to submit a ticket via web services.

Basically, all that’s required is passing one argument, the TicketID, and you can get back ALL of the ticket details.  It’s actually much more straightforward than I assumed that it would be.  One thing that I noticed is that reading through the OTRS dev documentation, the order that the ticket details are returned is different than what actually comes back.  That’s not really a big deal, since I moved everything into an array, with the various ticket details saved as a key/value pair.  More on that later.  The example that I have here only returns the ticket, but it is possible to return the associated articles also, but that will be for a later post.

The actual soapCall() function looks like this:

[code]
$TicketDetails = $client->__soapCall("Dispatch", 
array($username, $password,
"TicketObject", "TicketGet",
"TicketID", $TicketID,
));
[/code]

All I’m passing in is the SOAP username and password, along with the TicketID.  What it returns then is an XML-formatted response.  My page parses all of the returned values into an array, by using a foreach loop.  There are probably much more elegant solutions, but this works. Because of the way OTRS returns things, my foreach loop is a little janky.  Like I said, there’s probably a more elegant solution.

Without further ado, here’s the foreach loop:

[code]

$ticketInfo = array();
$i = 0;
foreach ($TicketDetails as $name => $value){
    if (false !== strpos($name, "s-gensym")){
        $temp[$i] = $value;
        $v = $temp[$i-1];
        //echo $value."<br>";
        if($i % 2 != 0){
            $ticketInfo[$v] = $value;
        }
        $i++;
    }
}
[/code]

As I said – a little janky.  Anyway, basically what OTRS does it returns the detail’s name as a variable, then the detail’s value as the NEXT variable like: “Age”, “123421”, “Title”, “This is my ticket title”, “TicketID”, “2”, so basically, you get the detail name, then the detail value.  To get these into a usable format, I’m basically creating 2 arrays, $temp[] and $ticketinfo[].  Since I’m simple-minded, the easiest thing for me to do was to create the $temp[] array so that I could basically use it as the “key” array when populating the $ticketinfo[] array.  Then what I’m doing is basically putting things into the $ticketinfo[] array as key/value pairs.  I take the current value – $value and put it in as the value and use the previously returned value, from $temp[], as the key.

I’ve included the complete commented code for the page below.  You can copy/paste it into a new, blank page, or you can download the zipped version of the file below.

[code]
<?PHP
error_reporting(E_ALL);
#### OTRS specific information ####
$url = "http://your_otrs_server/otrs/rpc.pl"; // URL for OTRS server
$username = "SOAP_username"; // SOAP username set in sysconfig
$password = "SOAP_password"; // SOAP password set in sysconfig
$TicketID = $_GET[id];
########################################################################
#### You don't have to change anything below here, although you can ####
########################################################################
#### Initialize new client session ####
$client = new SoapClient(
 null, 
 array(
 'location' => $url,
 'uri' => "Core",
 'trace' => 1,
 'login' => $username,
 'password' => $password,
 'style' => SOAP_RPC,
 'use' => SOAP_ENCODED
 )
);
#### Create and send the SOAP Function Call ####
$TicketDetails = $client->__soapCall("Dispatch", 
array($username, $password,
"TicketObject", "TicketGet",
"TicketID", $TicketID,
));
#### Get the SOAP response into an array as key/value pairs ####
# This is kind of janky, but what it does is parse out the #
# detail values in the SOAP response and writes them as a key/value #
# pair in the $ticketInfo[] array. #
$ticketInfo = array();
$i = 0;
foreach ($TicketDetails as $name => $value){ // explode the xml response
 if (false !== strpos($name, "s-gensym")){
 $temp[$i] = $value; 
 $v = $temp[$i-1]; 
 if($i % 2 != 0){ 
 $ticketInfo[$v] = $value; 
 }
 $i++;
 }
}
##############################################################################
#### The code below here is just to provide viewable proof that it worked ####
#### It can all be commented out or removed ####
##############################################################################
#### Return the SOAP request and response as xml-formatted text ####
print "<pre>\n"; 
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n"; 
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n"; 
print "</pre>";
echo"<hr width='200'>";
// Spew out the key/value pairs from the $ticketInfo[] array
foreach ($ticketInfo as $name => $value){
 echo "<b>".$name.":</b> ".$value."<br>";
}
?>
[/code]

Here’s the ticketGet zip file.  The formatting is obviously better in that version than what you see above.

Jeff Eske

 

Leave a Comment

If you would like to make a comment, please fill out the form below.

You must be logged in to post a comment.

© Copyright Jeff's Blog • Powered by Wordpress • Using Detour theme created by Brian Gardner.