Pages

Tuesday, November 8, 2011

Create a RT Ticket Using PHP and REST

We needed the ability to create tickets in RT using a PHP form. RT is a good support ticket system.

This is how I got it to work.

NOTE: This requires that you have a working install of RT.

Install HttpRequest (Ubuntu 11.10):
apt-get install php5-dev apt-get install libcurl3 apt-get install libmagic-dev pecl install pecl_http
NOTE: I had trouble with pecl_http on a 8.04 server. I had to complile it manually

Create a file (http.ini):
/etc/php5/conf.d/http.ini
Add this text to the http.ini file:
extension=http.so
Restart Apache
/etc/init.d/apache2 restart

Create the PHP file:

Create this file (I used this for a test):
/var/www/rt_test.php

Add this to the rt_test.php file:
 <?php  
   
 //RT Settings. Edit these values to suit your needs  
 $username = "username"; //RT User Login (User login for RT web interface)  
 $password = "password"; //RT User Password (User Password for RT web interface)  
 $requester_email = "user-email@example.com"; //RT User email  
 $ticket_queue = "General"; //The Queue name  
 $ticket_subject = "Ticket Subject"; //Subject on Ticket  
 $ticket_message = "Ticket Message"; //Message on Ticket  
 $rt_server = "rt-install.com"; //RT domain name  
   
 //Add a space after line breaks
 $ticket_message = str_replace("\n", "\n ", $ticket_message);

 //Complete URL  
 $url = "http://$rt_server/REST/1.0/ticket/new?user=$username&pass=$password";  
   
 //Create the Request  
 $request = new HttpRequest($url, HTTP_METH_POST);  
   
 //Create the POST Message  
 $message = "id: new\n";  
 $message .= "Queue: $ticket_queue\n";  
 $message .= "Requestor: $requester_email\n";  
 $message .= "Subject: $ticket_subject\n";  
 $message .= "Text: $ticket_message\n";  
   
 //Create an Array from the POST message  
 $post_data=array("content"=>$message);  
   
 //Add the POST Fields  
 $request->addPostFields($post_data);  
   
 try {  
   $response = $request->send()->getBody(); //Create the Ticket  
   echo $response; //Print the Response  
 } catch (HttpException $ex) {  
   echo $ex; //Print the Error  
 }  
   
 ?>  
Edit the configuration values to suit your needs. Run the PHP script a see if the ticket gets created.

Good Luck!

See: RT REST Wiki