title: osTicket API to create multiple tickets
body: <p>I am using oSTicket API to create a ticket on one website, as our company has instances where the same ticket needs to be duplicated to another website which is also running osticket I am receiving errors. What I am attempting and not sure where the error is, is to duplicate the API code. </p>
  
  <p>Below is the two codes placed into one. The information is drawn from a form on the previous page. If I do use the API for one ticket it works fine. I am using two different API keys</p>
  
  <pre><code>&lt;?php
  //eTrack case file create.
  
  date_default_timezone_set('Africa/Johannesburg');
  $today = date("Y-m-d");
  $time = date("H:i:s");
  if(!isset($_POST['submit']))
  {
  //This page should not be accessed directly. Need to submit the form.
      echo "Submitted";
  }
  
  $province = $_POST['province'];
  $usergroup = $_POST['usergroup'];
  $usermail = $_POST['usermail'];
  $topicId = $_POST['topicId'];
  
  // If 1, display things to debug.
  $debug="0";
  
  // You must configure the url and key in the array below.
  
  $config = array(
          'url'=&gt;'http://****.biz/incidents/api/tickets.json',  // URL to site.tld/api/tickets.json
          'key'=&gt;'****D66F84'  // API Key goes here
  );
  # NOTE: some people have reported having to use "http://your.domain.tld/api/http.php/tickets.json" instead.
  
  if($config['url'] === 'http://e-****.biz.tld/incidents/api/http.php/tickets.json') {
    echo "&lt;p style=\"color:red;\"&gt;&lt;b&gt;Error: No URL&lt;/b&gt;&lt;br&gt;You have not configured this script with your URL!&lt;/p&gt;";
    echo "Please edit this file ".__FILE__." and add your URL at line 18.&lt;/p&gt;";
    die();  
  }       
  if(IsNullOrEmptyString($config['key']) || ($config['key'] === '*******'))  {
    echo "&lt;p style=\"color:red;\"&gt;&lt;b&gt;Error: No API Key&lt;/b&gt;&lt;br&gt;You have not configured this script with an API Key!&lt;/p&gt;";
    echo "&lt;p&gt;Please log into System as an admin and navigate to: Admin panel -&gt; Manage -&gt; Api Keys then add a new API Key.&lt;br&gt;";
    echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.&lt;/p&gt;";
    die();
  }
  
  # Fill in the data for the new ticket, this will likely come from $_POST.
  # NOTE: your variable names in osT are case sensiTive. 
  # So when adding custom lists or fields make sure you use the same case
  # For examples on how to do that see Agency and Site below.
  $data = array(
      'name'      =&gt;      'Control',  // Client or Company Name
      'email'     =&gt;      "$usermail",  // Person opening the case file email
      'phone'     =&gt;      '**',  // Contact number of the person opening the case file. In this instance our Office Number
      'subject'   =&gt;      "$casetype - $client - $reg", // Case type description, Testing multiple POST replies to string
      'date2'     =&gt;      "$date",
      'time'      =&gt;      "$time",
      'message'   =&gt;      "
      Case Type:                  $casetype 
      Vehicle Registration:       $reg
      Vehicle Make:               $make
      Client:                     $name
      Where was it Taken:         $taken
      Reported by:                $name2 of $client
      Case Notes:
      $notes",  // test ticket body, aka Issue Details.
      'ip'        =&gt;      $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket.
      'topicId'   =&gt;      "$topicId", // the help Topic that you want to use for the ticket Help topic ID is linked to dropdown list
      'date'      =&gt;      "$date", //Reported date to Control room 
      'timerec'   =&gt;      "$time", // Reported time to control room
  
      'email'     =&gt;      "$usermail",
  
  );
  
  # more fields are available and are documented at:
  # https://github.com/osTicket/osTicket-1.8/blob/develop/setup/doc/api/tickets.md
  
  if($debug=='1') {
    print_r($data);
    die();
  }
  
  # Add in attachments here if necessary
  # Note: there is something with this wrong with the file attachment here it does not work.
  //$data['attachments'][] =
  //array('file.txt' =&gt;
    //      'data:text/plain;base64;'
      //        .base64_encode(file_get_contents('/file.txt')));  // replace ./file.txt with /path/to/your/test/filename.txt
  
  
  #pre-checks
  function_exists('curl_version') or die('CURL support required');
  function_exists('json_encode') or die('JSON support required');
  
  #set timeout
  set_time_limit(30);
  
  #curl post
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $config['url']);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8');
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $result=curl_exec($ch);
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  
  if ($code != 201)
      die('Unable to create ticket: '.$result);
  
  $ticket_id = (int) $result;
  
  # Continue onward here if necessary. $ticket_id has the ID number of the
  # newly-created ticket
  
  function IsNullOrEmptyString($question){
      return (!isset($question) || trim($question)==='');
  }
  ?&gt;
  
  &lt;?php
  //Recovery Assist case file create.
  
  date_default_timezone_set('Africa/Johannesburg');
  $today = date("Y-m-d");
  $time = date("H:i:s");
  #
  # Configuration: Enter the url and key. That is it.
  #  url =&gt; URL to api/task/cron e.g #  http://yourdomain.com/support/api/tickets.json
  #  key =&gt; API's Key (see admin panel on how to generate a key)
  #  $data add custom required fields to the array.
  #
  #  Originally authored by jared@osTicket.com
  #  Modified by ntozier@osTicket / tmib.net
  if(!isset($_POST['submit']))
  {
  //This page should not be accessed directly. Need to submit the form.
      echo "Submitted";
  }
  $name = $_POST['name'];
  
  $topicId = $_POST['topicId'];
  
  // If 1, display things to debug.
  $debug="0";
  
  // You must configure the url and key in the array below.
  
  $config = array(
          'url'=&gt;'http://***.co.za/report/api/tickets.json',  // URL to site.tld/api/tickets.json
          'key'=&gt;'****DA3C6'  // API Key goes here
  );
  # NOTE: some people have reported having to use "http://your.domain.tld/api/http.php/tickets.json" instead.
  
  if($config['url'] === 'http://***.co.za/report/api/http.php/tickets.json') {
    echo "&lt;p style=\"color:red;\"&gt;&lt;b&gt;Error: No URL&lt;/b&gt;&lt;br&gt;You have not configured this script with your URL!&lt;/p&gt;";
    echo "Please edit this file ".__FILE__." and add your URL at line 18.&lt;/p&gt;";
    die();  
  }       
  if(IsNullOrEmptyString($config['key']) || ($config['key'] === '****'))  {
    echo "&lt;p style=\"color:red;\"&gt;&lt;b&gt;Error: No API Key&lt;/b&gt;&lt;br&gt;You have not configured this script with an API Key!&lt;/p&gt;";
    echo "&lt;p&gt;Please log into System as an admin and navigate to: Admin panel -&gt; Manage -&gt; Api Keys then add a new API Key.&lt;br&gt;";
    echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.&lt;/p&gt;";
    die();
  }
  
  # Fill in the data for the new ticket, this will likely come from $_POST.
  # NOTE: your variable names in osT are case sensiTive. 
  # So when adding custom lists or fields make sure you use the same case
  # For examples on how to do that see Agency and Site below.
  $data = array(
      'name'      =&gt;      'Control',  // Client or Company Name
      'email'     =&gt;      "$usermail",  // Person opening the case file email
      'phone'     =&gt;      '****',  // Contact number of the person opening the case file. In this instance our Office Number
      'subject'   =&gt;      "$casetype - $client - $reg", // Case type description, Testing multiple POST replies to string
      'date2'     =&gt;      "$date",
      'time'      =&gt;      "$time",
      'message'   =&gt;      "
      Case Type:                  $casetype 
      Vehicle Registration:       $reg
      Vehicle Make:               $make
      Client:                     $name
      Where was it Taken:         $taken
      Reported by:                $name2 of $client
      Case Notes:
      $notes",  // test ticket body, aka Issue Details.
      'ip'        =&gt;      $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket.
      'topicId'   =&gt;      "$topicId", // the help Topic that you want to use for the ticket Help topic ID is linked to dropdown list
      'date'      =&gt;      "$date", //Reported date to Control room 
      'timerec'   =&gt;      "$time", // Reported time to control room
      'name'      =&gt;      "$name",
      'name2'     =&gt;      "$name2",
  
      'email'     =&gt;      "$usermail",
  
  );
  
  # more fields are available and are documented at:
  # https://github.com/osTicket/osTicket-1.8/blob/develop/setup/doc/api/tickets.md
  
  if($debug=='1') {
    print_r($data);
    die();
  }
  
  # Add in attachments here if necessary
  # Note: there is something with this wrong with the file attachment here it does not work.
  //$data['attachments'][] =
  //array('file.txt' =&gt;
    //      'data:text/plain;base64;'
      //        .base64_encode(file_get_contents('/file.txt')));  // replace ./file.txt with /path/to/your/test/filename.txt
  
  
  #pre-checks
  function_exists('curl_version') or die('CURL support required');
  function_exists('json_encode') or die('JSON support required');
  
  #set timeout
  set_time_limit(30);
  
  #curl post
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $config['url']);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8');
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $result=curl_exec($ch);
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  
  if ($code != 201)
      die('Unable to create ticket: '.$result);
  
  $ticket_id = (int) $result;
  
  # Continue onward here if necessary. $ticket_id has the ID number of the
  # newly-created ticket
  
  function IsNullOrEmptyString($question){
      return (!isset($question) || trim($question)==='');
  }
  ?&gt;
  </code></pre>
  
  <p>The error I receive is:</p>
  
  <p>Cannot redeclare IsNullOrEmptyString() (previously declared in /home/www/e-<strong><em>.biz/control/iframe/hollard/report_sub.php:350) in /home/www/e-</em></strong>.biz/control/iframe/hollard/report_sub.php on line 522</p>
  
  <p>Only thing I can think of is that due to duplicating the code it has conflicts on the second code.</p>
  
  <p>PS I have removed some of the POST code to make the code smaller </p>