Using the OTRS API via PHP/SOAP (Generic Interface)

Dont create your support topics here! No new topics with questions allowed!

Moderator: crythias

macjohnny
OTRS newbie
Posts: 5
Joined: 01 May 2014, 20:46
OTRS Version?: 3.3.0
Real Name: Esteban Marin
Company: Bithost GmbH

Using the OTRS API via PHP/SOAP (Generic Interface)

Postby macjohnny » 01 May 2014, 21:00

Hi everybody

I recently developed a connector script for using the OTRS API Generic Interface with PHP/SOAP. Feel free to use any of the code below for your own project.
You will have to create a WebService in OTRS. Once You have set it up, you can use PHP SOAP functions.

Code: Select all

<?php
namespace ClusterNetCore\Modules\App\API\External\OTRSConnector;
class OTRSConnectorMain extends \ClusterNetCore\Modules\Module\ModuleMain{
   
   
   protected $SOAPClientInstance;
   protected $LoggedIn = false;
   protected $Credentials = array();
   protected $GBSNetCredentials = array();
   protected $OTRSSpecialCredentials = array();
   
   public $SupportTickets = array();
   public $SupportTicketArticles = array();
   public $FAQCategories = array();
   public $FAQArticles = array();
   
   
   protected $SupportTicketsLoaded = false;
   protected $SupportTicketArticlesLoaded = false;
   protected $FAQCategoriesLoaded = false;
   protected $FAQArticlesLoaded = false;
   
   
   
   protected $HasUserData = true;
   
   protected $AdditionalFields = array(
      'Location' => 4,
      'Raumnummer' => 3,
      'Contact' => 5
   );
   
   public function ModuleConstructor($Parameters)
   {
      //https://yourhost.com/otrs/nph-genericinterface.pl
      $URL = $this->App->CredentialManagerInstance->GetFieldModuleInstance('App_ExternalApplication_Key')->GetExternalApplicationURL('OTRS', 'SOAP');
      

      $this->Credentials = $this->App->CredentialManagerInstance->GetExternalApplicationCredentials('OTRS');
      $this->GBSNetCredentials = $this->App->CredentialManagerInstance->GetExternalApplicationCredentials('GBSNet');
      
      $SessionCookie = array();
      
      $this->LoggedIn = true;
      
      $WebserviceName = $this->Configuration['WebserviceName'];
      
      try
      {
         $this->SOAPClientInstance = new \SoapClient(null, array(
               'location'=> $URL.'/Webservice/'.$WebserviceName.'/',
               'uri' => $this->Configuration['Namespace'],
               'trace' => 1,
               //'login' => $this->GBSNetCredentials['Username'],
               //'password'=> $this->GBSNetCredentials['Password'],
               'login' => $this->Configuration['SOAPUserCredentials']['Username'],
               'password'=> $this->Configuration['SOAPUserCredentials']['Password'],
               'style' => SOAP_RPC,
               'use' => SOAP_ENCODED
            )
         );
         
      }
      catch (\Exception $e) {
         echo "Exception Error: ",$e->getMessage(), "\n";
      }
      
      
   }
   
   public function LoadSupportTickets($FilterOptions = array(), $ReturnIDList = false)
   {
      if(!$this->SupportTicketsLoaded)
      {
         $time1 = microtime(1);
         
         
         if(isset($FilterOptions['StateType']))
         {
            $StateTypes = $FilterOptions['StateType'];
         }
         else
         {
            $StateTypes = array('Open', 'New', 'Closed', 'pending auto close+');
         }
         $TicketSearchOptions = array(
            new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
            new \SoapParam($this->Configuration['SOAPUserCredentials']['Username'], 'UserLogin'),
            
            new \SoapParam('ro', 'Permission'),
            new \SoapParam('ARRAY', 'Result'),
            new \SoapParam($StateTypes, 'StateType'),
            new \SoapParam('Changed', 'SortBy'),
         );
         
            
         
         if($this->App->Debug)
         {
                  $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".print_r($TicketSearchOptions, true));
         }
         try
         {
            $Result = $this->SOAPClientInstance->__soapCall("TicketSearch",
                  $TicketSearchOptions
               );
         }
         catch(\Exception $e)
         {
            echo "Exception Error: ",$e->getMessage(), "\n";
            print $this->SOAPClientInstance->__getLastRequest();
            print $this->SOAPClientInstance->__getLastResponse();
         }
         
         if(is_string($Result))
         {
            $TicketID = $Result;
            $Result = array(
               'TicketID' => array($TicketID),
            );
         }
         if($this->App->Debug)
         {
            $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
         }
         if($ReturnIDList === true)
         {
            return $Result['TicketID'];
         }
         else
         {
            $this->LoadSupportTicketsByID($Result['TicketID']);
            $this->SupportTicketsLoaded = true;
         }
         
      }
   }
   public function LoadSupportTicketsByID($IDList = array(), $LoadArticles = false)
   {
      if(!empty($IDList))
      {
         
         $time1 = microtime(1);
         $TicketRequestArray = array(
            new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
            
            new \SoapParam($IDList, 'TicketID'),
            new \SoapParam(1, 'DynamicFields'),
            new \SoapParam((int)$LoadArticles, 'AllArticles'),
            
         );
         $Result = $this->SOAPClientInstance->__soapCall("TicketGet",
            $TicketRequestArray
         );
         
         if($this->App->Debug)
         {
            $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time3: '.(microtime(1)-$time1));
         }
         
         if(is_object($Result))
         {
            $Result = array('Ticket' => array($Result));
         }
         if(!is_array($Result))
         {
            
            return array();
         }
         foreach($Result['Ticket'] as $RowObject)
         {
            $RowObject->TicketNumber = number_format($RowObject->TicketNumber, 0, '.','');
            $this->SupportTickets[] = (array)$RowObject;
         }
      }
   }
   public function SaveSupportTicket($TicketData, $ArticleData = array(), $Attachments = array())
   {
      $DefaultTicketData = array(
         'TicketID' => 0,
         'Title'=> 't',
         'From'=> $this->Credentials['Username'],
         'Queue'=> '.SPOC',
         'Lock' => 'unlock',
         'Priority' => '3 normal',
         'State'=> 'new',
         'CustomerUser' => $this->Credentials['Username'],
         'OwnerID' => 1,
         'Location' => '',
         'Raumnummer' => '',
         'Contact' => '',
         
      );
      $DefaultArticleData = array(
         'From' => $this->GBSNetCredentials['Username'],
         'Subject' => '',
         'Body' => '',
         'MimeType' => 'text/html',
         'Charset'=> 'utf8',
      );
      $DefaultAttachmentData = array(
         'Content' => '',
         'ContentType' => 'application/download; charset=utf8',
         'Filename' => '',
      );
      $TicketData = array_merge($DefaultTicketData, $TicketData);
      $ArticleData = array_merge($DefaultArticleData, $ArticleData);
      $AttachmentsToAdd = array();
      foreach($Attachments as &$Attachment)
      {
         $Attachment = array_merge($DefaultAttachmentData, $Attachment);
         if(!empty($Attachment['Filename']))
         {
            $AttachmentsToAdd[] = &$Attachment;
         }
      }
      $TicketData['CustomerUser'] = $this->Credentials['Username'];
      
      
      $TicketDynamicFieldRequestArray = array(
            
      );      
      foreach($this->AdditionalFields as $Key => $FieldID)
      {
         if(isset($TicketData['DynamicField_'.$Key]) && !empty($TicketData['DynamicField_'.$Key]))
         {
            $TicketDynamicFieldRequestArray[] = array(
               'Name' => $Key,
               'Value' => $TicketData['DynamicField_'.$Key],
            );
         }
      }
      
      $time1 = microtime(1);
      
      $TicketRequestArray = array(
         new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
         new \SoapParam($this->Configuration['SOAPUserCredentials']['Username'], 'UserLogin'),
         new \SoapParam($this->Credentials['Password'], "Password"),
      );
         
         
         
      
      if($TicketData['TicketID']>0)
      {
         $TicketID = $TicketData['TicketID'];
         $TicketRequestArray[] = new \SoapParam($TicketID, 'TicketID');
         $TicketRequestArray[] = new \SoapParam(array(
            'State' => 'open'
         ), 'Ticket');
         $Action = 'TicketUpdate';
         
      }
      else
      {
         $Action = 'TicketCreate';
         $TicketRequestArray[] = new \SoapParam($TicketDynamicFieldRequestArray, 'DynamicField');
         $TicketRequestArray[] = new \SoapParam($TicketData, 'Ticket');
         
         if(empty($ArticleData['Body']))
         {
            die();
         }
      }
      if(!empty($ArticleData['Body']))
      {
         $TicketRequestArray[] = new \SoapParam($ArticleData, 'Article');
      }
      if(!empty($AttachmentsToAdd))
      {
         $TicketRequestArray[] = new \SoapParam($AttachmentsToAdd, 'Attachment');
      }
      
      
      $Response = $this->SOAPClientInstance->__soapCall($Action,
         $TicketRequestArray
      );
      if($this->App->Debug)
      {
         $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
      }
      if($this->App->Debug)
      {
         $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".print_r($Response, true));
      }
      
      return (array)$Response;
      
   }
   
   
   public function LoadFAQCategories()
   {
      if(!$this->FAQCategoriesLoaded)
      {
         $time1 = microtime(1);
         
         
         
         $RequestOptions = array(
            new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
            
         );
         
         try
         {
            $Result = $this->SOAPClientInstance->__soapCall("FAQPublicCategoryList",
                  $RequestOptions
               );
            
         }
         catch(\Exception $e)
         {
            echo "Exception Error: ",$e->getMessage(), "\n";
            print $this->SOAPClientInstance->__getLastRequest();
            print $this->SOAPClientInstance->__getLastResponse();
         }
         
         
         if($this->App->Debug)
         {
            $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
         }
         
         if(isset($Result['Category']))
         {
            foreach($Result['Category'] as &$Category)
            {
               $Category = (array)$Category;
            }
            $this->FAQCategories = $Result['Category'];
            $this->FAQCategoriesLoaded = true;
         }
         
      }
   }
   public function LoadFAQArticles($FilterOptions = array(), $ReturnIDList = false, $NoAttachments = true)
   {
      if(!$this->FAQArticlesLoaded)
      {
         $time1 = microtime(1);
         
         
         $TicketSearchOptions = array(
            new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
         );
         if(isset($FilterOptions['CategoryID']))
         {
            
            if(!is_array($FilterOptions['CategoryID']))
            {
               $FilterOptions['CategoryID'] = array($FilterOptions['CategoryID']);
            }
            $TicketSearchOptions[] = new \SoapParam($FilterOptions['CategoryID'], 'CategoryID');
         }
         
         if(isset($FilterOptions['SearchKeywords']))
         {
            $TicketSearchOptions[] = new \SoapParam('*'.implode(' ', $FilterOptions['SearchKeywords']).'*', 'What');
            
         }
         
         if($this->App->Debug)
         {
                  $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".print_r($TicketSearchOptions, true));
         }
         try
         {
            $Result = $this->SOAPClientInstance->__soapCall("FAQPublicFAQSearch",
                  $TicketSearchOptions
               );
         }
         catch(\Exception $e)
         {
            echo "Exception Error: ",$e->getMessage(), "\n";
            print $this->SOAPClientInstance->__getLastRequest();
            print $this->SOAPClientInstance->__getLastResponse();
         }
         
         if(is_string($Result))
         {
            $TicketID = $Result;
            $Result = array(
               'ID' => array($TicketID),
            );
         }
         if($this->App->Debug)
         {
            $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
         }
         if($ReturnIDList === true)
         {
            return $Result['ID'];
         }
         else
         {
            $this->LoadFAQArticlesByID($Result['ID'], $NoAttachments);
            $this->FAQArticlesLoaded = true;
         }
      }
   }
   public function LoadFAQArticlesByID($IDList = array(), $NoAttachments = true)
   {
      if(!empty($IDList))
      {
         
         $time1 = microtime(1);
         $TicketRequestArray = array(
            new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
            new \SoapParam($this->Credentials['Password'], "Password"),
            new \SoapParam(implode(',',$IDList), 'ItemID'),
            new \SoapParam((int)!$NoAttachments, "GetAttachments"),
            
         );
         $Result = $this->SOAPClientInstance->__soapCall("FAQPublicFAQGet",
            $TicketRequestArray
         );
         
         if($this->App->Debug)
         {
            $this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time3: '.(microtime(1)-$time1));
         }
         
         if(is_object($Result))
         {
            
            $Result = array('FAQItem' => array($Result));
         }
         if(!is_array($Result))
         {
            
            return array();
         }
         
         foreach($Result['FAQItem'] as $Key=>$RowObject)
         {
            $this->FAQArticles[] = (array)$RowObject;
         }
      }
   }
   
}
?>


Cheers,

macjohnny

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 11 Aug 2016, 07:50

Can you please explain how this code works? I am trying to update owner of the ticket using soap api via php. and i am stuck in this. can you help me?

macjohnny
OTRS newbie
Posts: 5
Joined: 01 May 2014, 20:46
OTRS Version?: 3.3.0
Real Name: Esteban Marin
Company: Bithost GmbH

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby macjohnny » 11 Aug 2016, 10:42

szn0007 wrote:Can you please explain how this code works? I am trying to update owner of the ticket using soap api via php. and i am stuck in this. can you help me?


first, you need to initialize a soapclient, so you do

Code: Select all

$SOAPClientInstance = new \SoapClient(null, array(
               'location'=> $URL.'/Webservice/'.$WebserviceName.'/',
               'uri' => $this->Configuration['Namespace'],
               'trace' => 1,
               'login' => $this->Configuration['SOAPUserCredentials']['Username'],
               'password'=> $this->Configuration['SOAPUserCredentials']['Password'],
               'style' => SOAP_RPC,
               'use' => SOAP_ENCODED
            )
         );

then you can have a look at

Code: Select all

function SaveSupportTicket()
, you can change the data of the ticket and it will save it.
if you need further assistance, you can contact me here: esteban dot marin _at_ bithost dot ch

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 12 Aug 2016, 08:06

Till now i have tried this

$url = "https://my.url.com/otrs/rpc.pl"; // URL for OTRS server
$username = "ctdeveloper"; // SOAP username set in sysconfig
$password = "ctdeveloper"; // SOAP password set in sysconfig



### Form Fields

$new_owner =$_POST['new_owner'];
$subject =$_POST['subject'];
$text = $_POST['text'];
$note_type = $_POST['note_type'];


#### 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
)
);

$ticket_update = $client->__soapCall("Dispatch",
array($username, $password,
"TicketObject" => "TicketUpdate",
"TicketID" => "$ticket_id",
"Title" => "$subject",
"Owner" => $new_owner,
"QueueID" => 4,
"CustomerUserLogin", "ctdeveloper",
)
);

##### Create an article with the info. The function returns an Article ID ####
$ArticleID = $client->__soapCall("Dispatch",
array($username, $password,
"TicketObject", "ArticleCreate",
"ArticleType", $note_type,
"Subject", $subject,
"ContentType", "text/plain; charset=ISO-8859-1",
"Body", $text,


)
);

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 12 Aug 2016, 08:07

I am weak in object oriented methods so its hard for me to understand your code.

macjohnny
OTRS newbie
Posts: 5
Joined: 01 May 2014, 20:46
OTRS Version?: 3.3.0
Real Name: Esteban Marin
Company: Bithost GmbH

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby macjohnny » 12 Aug 2016, 12:50

you need to use the URL for the generic interface, i.e.

http//my.url.com/otrs/nph-genericinterface.pl

macjohnny
OTRS newbie
Posts: 5
Joined: 01 May 2014, 20:46
OTRS Version?: 3.3.0
Real Name: Esteban Marin
Company: Bithost GmbH

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby macjohnny » 12 Aug 2016, 13:04

also, you need to set up a WebService in OTRS. See https://otrs.github.io/doc/manual/admin ... ebservices

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 14 Aug 2016, 07:36

Please find the attatchments. I have attached the screenshot of webservice that i created. and here is my code. i am getting errors
Fatal error: Uncaught SoapFault exception: . What am i doing wrong. i need to update the tickets. If possibe can u provide me complete ticket update code in php via soap api? It will be a big help

Code: Select all

$url      = "https://my_url/otrs/nph-genericinterface.pl";  // URL for OTRS server

            $username = "ctdeveloper";  // SOAP username set in sysconfig
            $password = "ctdeveloper";  // SOAP password set in sysconfig



            ### Form Fields

            $new_owner =$_POST['new_owner'];
            $subject =$_POST['subject'];
            $text = $_POST['text'];
            $note_type = $_POST['note_type'];


            #### Initialize new client session ####
            $client = new SoapClient(
                null,
                array(
                    'location'  => $url.'/Webservice/soap',
                    'uri'       => "Core",
                    'trace'     => 1,
                    'login'     => $username,
                    'password'  => $password,
                    'style'     => SOAP_RPC,
                    'use'       => SOAP_ENCODED
                )
            );

            $ticket_update = $client->__soapCall("Dispatch",
                array($username, $password,
                "TicketObject" => "TicketUpdate",
                    "TicketID" => "$ticket_id",
                    "Title" => "$subject",
                    "Owner" => $new_owner,
                    "QueueID" => 4,
                    "CustomerUserLogin", "ctdeveloper",
                )
            );

            ##### Create an article with the info. The function returns an Article ID ####
            $ArticleID = $client->__soapCall("Dispatch",
                array($username, $password,
                    "TicketObject",   "ArticleCreate",
                    "ArticleType",    $note_type,
                    "Subject",        $subject,
                    "ContentType",    "text/plain; charset=ISO-8859-1",
                    "Body",           $text,


                )
            );
You do not have the required permissions to view the files attached to this post.

jojo
Moderator
Posts: 14088
Joined: 26 Jan 2007, 14:50
OTRS Version?: Git Master
Contact:

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby jojo » 14 Aug 2016, 11:41

You'll find an example (in Perl) here: https://github.com/OTRS/otrs/tree/maste ... ebservices
"Production": OTRS Business Solution™ 6, STORM powered by OTRS
"Testing": OTRS git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com :: Share your ideas

macjohnny
OTRS newbie
Posts: 5
Joined: 01 May 2014, 20:46
OTRS Version?: 3.3.0
Real Name: Esteban Marin
Company: Bithost GmbH

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby macjohnny » 15 Aug 2016, 06:48

hey

to update a ticket, you need to

Code: Select all

   $URL = 'https://my.url.com/otrs/nph-genericinterface.pl';
   $namespace = 'https://my.url.com/otrs/GenericInterface/actions';
   
   // initialize a SoapClient instance
   $SOAPClientInstance = new \SoapClient(null, array(
               'location'=> $URL,
               'uri' => $namespace,
               'trace' => 1,
               'login' => $username,
               'password'=> $password,
               'style' => SOAP_RPC,
               'use' => SOAP_ENCODED
            )
         );
   
   // set the request parameters as an array of SoapParam instances
      $TicketRequestArray = array(
         new \SoapParam($username, 'CustomerUserLogin'),
         new \SoapParam($soap_username, 'UserLogin'),
         new \SoapParam($password, "Password"),
      );

         $TicketRequestArray[] = new \SoapParam($TicketID, 'TicketID');
         $TicketRequestArray[] = new \SoapParam(array(
            'State' => 'open'
         ), 'Ticket');
         $Action = 'TicketUpdate';
         
         $Response = $SOAPClientInstance->__soapCall($Action,
         $TicketRequestArray
      );


szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 15 Aug 2016, 12:30

jojo wrote:You'll find an example (in Perl) here: https://github.com/OTRS/otrs/tree/maste ... ebservices



Can you write this in PHP. I dont know perl

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 15 Aug 2016, 12:34

i got following error.
Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in /Applications/XAMPP/xamppfiles/htdocs/portal/application/modules/otrs/controllers/otrs.php:472 Stack trace: #0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'https://otrs.cl...', 'https://otrs.cl...', 1, 0) #1 /Applications/XAMPP/xamppfiles/htdocs/portal/application/modules/otrs/controllers/otrs.php(472): SoapClient->__soapCall('TicketUpdate', Array) #2 /Applications/XAMPP/xamppfiles/htdocs/portal/system/core/CodeIgniter.php(359): Otrs->owner_update('46939') #3 /Applications/XAMPP/xamppfiles/htdocs/portal/index.php(203): require_once('/Applications/X...') #4 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/portal/application/modules/otrs/controllers/otrs.php on line 472

jojo
Moderator
Posts: 14088
Joined: 26 Jan 2007, 14:50
OTRS Version?: Git Master
Contact:

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby jojo » 15 Aug 2016, 13:57

szn0007 wrote:
jojo wrote:You'll find an example (in Perl) here: https://github.com/OTRS/otrs/tree/maste ... ebservices



Can you write this in PHP. I dont know perl


I don't code PHP: A senior developer should be able to understand the provided code and other files.
"Production": OTRS Business Solution™ 6, STORM powered by OTRS
"Testing": OTRS git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com :: Share your ideas

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 16 Aug 2016, 08:40

macjohnny wrote:hey

to update a ticket, you need to

Code: Select all

   $URL = 'https://my.url.com/otrs/nph-genericinterface.pl';
   $namespace = 'https://my.url.com/otrs/GenericInterface/actions';
   
   // initialize a SoapClient instance
   $SOAPClientInstance = new \SoapClient(null, array(
               'location'=> $URL,
               'uri' => $namespace,
               'trace' => 1,
               'login' => $username,
               'password'=> $password,
               'style' => SOAP_RPC,
               'use' => SOAP_ENCODED
            )
         );
   
   // set the request parameters as an array of SoapParam instances
      $TicketRequestArray = array(
         new \SoapParam($username, 'CustomerUserLogin'),
         new \SoapParam($soap_username, 'UserLogin'),
         new \SoapParam($password, "Password"),
      );

         $TicketRequestArray[] = new \SoapParam($TicketID, 'TicketID');
         $TicketRequestArray[] = new \SoapParam(array(
            'State' => 'open'
         ), 'Ticket');
         $Action = 'TicketUpdate';
         
         $Response = $SOAPClientInstance->__soapCall($Action,
         $TicketRequestArray
      );



I got fatal error. i think i am having error in namespace and url .

szn0007
OTRS newbie
Posts: 28
Joined: 10 Jul 2016, 08:59
OTRS Version?: 3.1.12
Real Name: Sujan Shrestha
Company: Classictech

Re: Using the OTRS API via PHP/SOAP (Generic Interface)

Postby szn0007 » 19 Aug 2016, 11:07

A big thanks to @macjohnny who has been a big help. Without whom my problem would not have been solved.
Here is my final code for updating owners or updating tickets in general.

$URL = 'https://your-url.com/otrs/nph-genericinterface.pl/Webservice/WebserviceName'; //webserviceName is the name of webservice created from admin panel of otrs
$namespace = 'https://your-url.com/otrs/GenericInterface/actions'; //namespace of soap config

$username = "username"; //// SOAP username set in sysconfig
$password = "password"; //// SOAP password set in sysconfig

// initialize a SoapClient instance
$SOAPClientInstance = new \SoapClient(null, array(
'location'=> $URL,
'uri' => $namespace,
'trace' => 1,
'login' => $username,
'password'=> $password,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED,

)
);


// set the request parameters as an array of SoapParam instances
$TicketRequestArray = array(

new \SoapParam('username', 'UserLogin'), //user username
new \SoapParam('password', "Password"),// user password
);

$TicketRequestArray[] = new \SoapParam('123', 'TicketID');// ID of ticket which is to be updated
$TicketRequestArray[] = new \SoapParam(array(
'State' => 'open',
'OwnerID' => $new_owner, // you can add which parameters to update
), 'Ticket');
$Action = 'TicketUpdate';

$Response = $SOAPClientInstance->__soapCall($Action,
$TicketRequestArray
);

print "<pre>\n";
print "Request :\n".htmlspecialchars($SOAPClientInstance->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($SOAPClientInstance->__getLastResponse())."\n";
print "</pre>";


Return to “Howtos”

Who is online

Users browsing this forum: No registered users and 1 guest