Create a ticket in OTRS from PHP via RPC

From OtterHub - OTRS Community Wiki
Jump to: navigation, search

This example shows how to create a ticket in OTRS via the OTRS rpc interface from PHP. Please note that I'm not a PHP programmer AT ALL, and this script is just meant as a proof-of-concept. You should at the very least add some error catching to the script.

You could install the script on a machine running PHP. To get it working, I had to install the package php-soap on the machine running the PHP script in order to be able to talk SOAP from PHP.

In order to enable the RPC interface in OTRS you have to set a user name and password under Admin > SysConfig > Framework > Core::Soap. Also, you might want to verify that the Perl module SOAP::Lite is installed. For more information on the API calls please see the API documentation.

<?PHP
error_reporting(E_ALL);

# Please define the connection information here:
$url      = "http://example.com/otrs/rpc.pl";
$username = "some_user";
$password = "some_pass";
$title    = "My Test Ticket via SOAP and PHP";
$from     = "me@example.com";


echo "<html>\n";
echo "<head>\n";
echo "<title>Test SOAP-Interface</title>\n";
echo "</head>\n";
echo "<body>\n";
echo "<h1>Test SOAP-interface of OTRS</h1>\n";

# Set up a new SOAP connection:
$client = new SoapClient(null, array('location'  =>
$url,
                                     'uri'       => "Core",
                                     'trace'     => 1,
                                     'login'     => $username,
                                     'password'  => $password,
                                     'style'     => SOAP_RPC,
                                     'use'       => SOAP_ENCODED));

# Create a new ticket. The function returns the Ticket ID.
$TicketID = $client->__soapCall("Dispatch", array($username, $password,
"TicketObject", "TicketCreate", 
"Title",        $title, 
"Queue",        "Postmaster", 
"Lock",         "Unlock", 
"PriorityID",   2, 
"State",        "new", 
"CustomerUser", $from, 
"OwnerID",      1, 
"UserID",       1,
));

# A ticket is not usefull without at least one article. The function
# returns an Article ID. 
$ArticleID = $client->__soapCall("Dispatch", 
array($username, $password,
"TicketObject",   "ArticleCreate",
"TicketID",       $TicketID,
"ArticleType",    "webrequest",
"SenderType",     "customer",
"HistoryType",    "WebRequestCustomer",
"HistoryComment", "created from PHP",
"From",           $from,
"Subject",        $title,
"ContentType",    "text/plain; charset=ISO-8859-1",
"Body",           "This is the body",
"UserID",         1,
"Loop",           0,
"AutoResponseType", 'auto reply',
"OrigHeader", array(
        'From' => $from,
        'To' => 'Postmaster',
        'Subject' => $title,
        'Body' => "This is the body"
    ),
));



# Use the Ticket ID to retrieve the Ticket Number.
$TicketNr = $client->__soapCall("Dispatch", 
array($username, $password,
"TicketObject",   "TicketNumberLookup",
"TicketID",       $TicketID,
));

# Make sure the ticket number is not displayed in scientific notation
# See http://forums.otrs.org/viewtopic.php?f=53&t=5135
$big_integer = 1202400000; 
$Formatted_TicketNr = number_format($TicketNr, 0, '.', ''); 

# Print the info to the screen.
echo "<p>You have just created ticket id $TicketID with article id "
."$ArticleID. The ticket number is $Formatted_TicketNr.</p>\n";
echo "</body>\n";
echo "</html>\n";

?>

If you have any questions, please feel free to ask them in the forum or on the mailing lists.

Some more examples: English: http://www.iniy.org/?p=20 German: http://www.iniy.org/?p=20&lang=de

.Net example is here: http://forums.otrs.org/viewtopic.php?f=37&t=6239

--Mike B 15:17, 19. Nov. 2009 (UTC)--