Create tickets from PHP using SOAP

English! place to talk about development, programming and coding
Post Reply
miroiuso
Znuny newbie
Posts: 3
Joined: 15 May 2012, 10:14
Znuny Version: 3.1.4

Create tickets from PHP using SOAP

Post by miroiuso »

Hello all,

First of all I would like to apolagize in advance if this topic is discussed somewhere else. If it is, please point me in that direction. Thank you.

Ok, so we are trying to create ticket from a php web page using the following code bellow:

Code: Select all

<?php
$otrs_title = "TEST_";
$otrs_subject = "TEST_";
$otrs_queue = "Raw";
$otrs_from = "some_email";
$otrs_email = "another_email";
$otrs_user = "soap_user";
$otrs_pass = "soap_password";
$otrs_url = "http://our_otrs_installment_link/otrs/rpc.pl";
$in_customerid = 4;
$in_customeruser = "some_other_email";
$in_owner = 3;

$soapclient = new SoapClient(null, array('location'  => $otrs_url,
                                      'uri'       => "Core",
                                      'trace'     => 1,
                                      'login'     => $otrs_user,
                                      'password'  => $otrs_pass,
                                      'style'     => SOAP_RPC,
                                      'use'       => SOAP_ENCODED));

              # creating a ticket number
              $ticketnr = $soapclient->__soapCall("Dispatch", array($otrs_user, $otrs_pass, "TicketObject", "TicketCreateNumber"));

              #php changes long numbers to 123E+10 notation to prevent this screwing up our ticketnumbers we convert notation
              #it into normal plain numbers. but only if not string,because sometimes you have a string addition like XY123 on your
              #ticket numbers

              if(! is_string($ticketnr) ) $ticketnr = number_format($ticketnr,0, '.', '');

              // # create the new ticket
              $otrs_userid = $soapclient->__soapCall("Dispatch", array($otrs_user, $otrs_pass, "UserObject", "UserLookup", "UserLogin", $in_owner));
              $ticketid = $soapclient->__soapCall("Dispatch", array($otrs_user, $otrs_pass, "TicketObject", "TicketCreate",
													"TN", $ticketnr,
													"Title", $otrs_title,
													"Queue", $otrs_queue,
													"Lock" , "unlock",
													"PriorityID", 3,
													"State" , "new",
													"CustomerId", $in_customerid, 
													"CustomerUser", $in_customeruser,
													"OwnerID",     $otrs_userid,
													"UserID", $otrs_userid));

              $articleid = $soapclient->__soapCall("Dispatch", array($otrs_user, $otrs_pass, "TicketObject", "ArticleSend",
                                         "TicketID"         , $ticketid,
                                         "ArticleType"      , "email-external",  # email-external|email-internal|phone|fax|...
                                         "SenderType"       , "agent", #agent|system|customer
                                         "From"             , $otrs_from,
                                         "To"               , $otrs_email,
                                         "Subject"          , "[Ticket#".$ticketnr."] ".$otrs_subject,
                                         "Body"             , "HAULES",
                                         "ContentType"      , "text/plain; charset=utf-8",
                                         "Charset"          , "utf-8",
                                         "HistoryType"      , "EmailCustomer", #EmailCustomer|Move|AddNote|PriorityUpdate|WebRequestCustomer|...
                                         "HistoryComment"   , "generated by OTRSInterface-Class",
                                         "UserID"           , $otrs_userid,
                                         "NoAgentNotify"    , 1, # if you don't want to send agent notifications
                                         "MimeType"         , "text/plain",
                                         "Loop"             , 0 # auto reject|auto follow up|auto follow up|auto remove));

              $linkid = $soapclient->__soapCall("Dispatch", array($otrs_user, $otrs_pass, "LinkObject", "LinkAdd",
												  "SourceObject", "Ticket",
												  "SourceKey", "0601158",  #0601158 is the tn of a pre-existing ticket to link to.
												  "TargetObject", "Ticket",
												  "TargetKey" , $ticketid,
												  "Type", "ParentChild",
												  "Direction", "Target",
												  "State" , "Valid",
												  "UserId", $otrs_userid));

echo "$ticketnr";
echo "<br>";
echo "$ticketid";
?>
The script produces a ticket number and it prints it on the screen, but no ticket Id. If I look in the DB (which is a MySQL DB) I don't find any ticket there.
The strange behaviour I noticed is that the ticket number keeps incrementing itself. And I also created a ticket from the OTRs web interface and the ticket number it took was exactly the next number in line from the ticket number the script printed. (e.g: the scripts prints 1000047, the ticket number of the ticket created in OTRs has 1000048).

What am I missing here ? Do I need to run an extra script ? Is the script wrong ? Any help would be great.

PS: OTRs version is 3.1.4. SOAP module is enabled and so are the username and password in SysConfig Core::SOAP

Thank you and regards,
Sorin M.
jeske
Znuny newbie
Posts: 10
Joined: 10 Jan 2012, 17:44
Znuny Version: 3.1.2
Real Name: jeff
Company: Creighton University
Location: Creighton University, Omaha, NE
Contact:

Re: Create tickets from PHP using SOAP

Post by jeske »

Sorin,

It appears that when they fixed a bug in the web services between 3.1.2 and 3.1.4, they changed some of the required information for submitting tickets via web services. Through a fair amount of trial and error, I worked out what it seems to take. This is far from official, but it worked for me.

Below worked in 3.1.2

Code: Select all


$TicketID = $client->__soapCall(
“Dispatch”, array($username, $password,
“TicketObject”, “TicketCreate”,
“Title”,        $title,
“Queue”,        “Raw”,
“Lock”,         “Unlock”,
“PriorityID”,   1,
“State”,        “new”,
“CustomerUser”, $from,
“CustomerID”, “some ID”,
“OwnerID”,      1,
“UserID”,       1,
));

Code that should work for 3.1.4

Code: Select all


$TicketID = $client->__soapCall(
“Dispatch”, array($username, $password,
“TicketObject”, “TicketCreate”,
“Title”,        $title,
“TypeID”,    2, // id from database
“QueueID”,   2, // id from database
“LockID”,  1, // id from database
“PriorityID”,   2, // id from database
“State”,        “new”,
“CustomerUser”, $from,
“CustomerID”, “some ID”,
“OwnerID”,      1,
“UserID”,       1,
));
Hope this helps.

Jeff
OTRS 3.1 on Linux with MySQL database connected to an Active Directory for Agents and Customers.
Post Reply