2017-07-14 88 views
0

我正在使用oSTicket API在一个网站上创建票证,因为我们公司有相同票证需要复制到另一个网站的实例,该网站也在运行我收到错误。我正在尝试并且不确定错误在哪里,是复制API代码。osTicket API创建多张票

下面是两个代码放在一个。信息是从前一页的表格中绘制的。如果我确实使用一张票的API,它工作正常。我使用两个不同的API密钥

<?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'=>'http://****.biz/incidents/api/tickets.json', // URL to site.tld/api/tickets.json 
     'key'=>'****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 "<p style=\"color:red;\"><b>Error: No URL</b><br>You have not configured this script with your URL!</p>"; 
    echo "Please edit this file ".__FILE__." and add your URL at line 18.</p>"; 
    die(); 
}  
if(IsNullOrEmptyString($config['key']) || ($config['key'] === '*******')) { 
    echo "<p style=\"color:red;\"><b>Error: No API Key</b><br>You have not configured this script with an API Key!</p>"; 
    echo "<p>Please log into System as an admin and navigate to: Admin panel -> Manage -> Api Keys then add a new API Key.<br>"; 
    echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.</p>"; 
    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'  =>  'Control', // Client or Company Name 
    'email'  =>  "$usermail", // Person opening the case file email 
    'phone'  =>  '**', // Contact number of the person opening the case file. In this instance our Office Number 
    'subject' =>  "$casetype - $client - $reg", // Case type description, Testing multiple POST replies to string 
    'date2'  =>  "$date", 
    'time'  =>  "$time", 
    'message' =>  " 
    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'  =>  $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket. 
    'topicId' =>  "$topicId", // the help Topic that you want to use for the ticket Help topic ID is linked to dropdown list 
    'date'  =>  "$date", //Reported date to Control room 
    'timerec' =>  "$time", // Reported time to control room 

    'email'  =>  "$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' => 
    //  '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)===''); 
} 
?> 

<?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 => URL to api/task/cron e.g # http://yourdomain.com/support/api/tickets.json 
# key => API's Key (see admin panel on how to generate a key) 
# $data add custom required fields to the array. 
# 
# Originally authored by [email protected] 
# Modified by [email protected]/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'=>'http://***.co.za/report/api/tickets.json', // URL to site.tld/api/tickets.json 
     'key'=>'****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 "<p style=\"color:red;\"><b>Error: No URL</b><br>You have not configured this script with your URL!</p>"; 
    echo "Please edit this file ".__FILE__." and add your URL at line 18.</p>"; 
    die(); 
}  
if(IsNullOrEmptyString($config['key']) || ($config['key'] === '****')) { 
    echo "<p style=\"color:red;\"><b>Error: No API Key</b><br>You have not configured this script with an API Key!</p>"; 
    echo "<p>Please log into System as an admin and navigate to: Admin panel -> Manage -> Api Keys then add a new API Key.<br>"; 
    echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.</p>"; 
    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'  =>  'Control', // Client or Company Name 
    'email'  =>  "$usermail", // Person opening the case file email 
    'phone'  =>  '****', // Contact number of the person opening the case file. In this instance our Office Number 
    'subject' =>  "$casetype - $client - $reg", // Case type description, Testing multiple POST replies to string 
    'date2'  =>  "$date", 
    'time'  =>  "$time", 
    'message' =>  " 
    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'  =>  $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket. 
    'topicId' =>  "$topicId", // the help Topic that you want to use for the ticket Help topic ID is linked to dropdown list 
    'date'  =>  "$date", //Reported date to Control room 
    'timerec' =>  "$time", // Reported time to control room 
    'name'  =>  "$name", 
    'name2'  =>  "$name2", 

    'email'  =>  "$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' => 
    //  '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)===''); 
} 
?> 

我收到的错误是:

不能重新声明IsNullOrEmptyString()(在/ home/WWW/E-.BIZ /控制/ IFRAME/hollard先前声明/report_sub.php:350)在/ home/WWW/E- .BIZ /控制/ IFRAME/hollard/report_sub.php上线522

只有我能想到的事情是,由于复制它的代码在第二个代码上有冲突。

PS我已删除了部分POST码,使代码更小

回答

0

IsNullOrEmptyString功能已定义。

为了避免这种错误,使用:

if (!function_exists('IsNullOrEmptyString')) { 
    function IsNullOrEmptyString($question){ 
     return (!isset($question) || trim($question)===''); 
    } 
} 

虽然你,要特别注意对为什么你得到这个错误。它通常表示应用程序逻辑中出现错误。

这是从来没有好主意,只是重复代码。尝试查看共同/相同的部分,并将它们作为单独的函数提取。